0

I am using CMake with C++ and have a shared library in another project. I have added the lines to my CMake file:

find_library(lib1_location NAMES myLib)
message("myLib at: ${lib1_location}")
target_link_libraries(myProject ${lib1_location})

and it successfully finds the library.

In my C++ header file, I try to use it just with:

#include "myLib.h"

but it can't find it. What more do I need to do to use this shared library? Thanks for your patience.

bkepley
  • 71
  • 1
  • 3

1 Answers1

0

Your CMake code will correctly find the myLib library file, but not the library's header files. I imagine you want to add these headers to the include directories during compilation of your project. Similar to find_library(), you can use find_path() to get the include directory containing the header file(s):

# Look for a path containing the 'myLib.h' header file, using a 
#   hard-coded hint path if necessary.
find_path(lib1_include_directory "myLib.h"
    PATHS "/path/to/lib1/headers"
)
message("myLib headers at: ${lib1_include_directory}")
# Use this path as an include directory during compilation of myProject.
target_include_directories(myProject PRIVATE ${lib1_include_directory})

find_library(lib1_location NAMES myLib)
message("myLib at: ${lib1_location}")
target_link_libraries(myProject ${lib1_location})
Kevin
  • 16,549
  • 8
  • 60
  • 74