I am making a custom library that I want to be installable for users. However, when I try to use my own library in a cmake executable, I get a build error saying that the library headers were not found.
The library CMakeLists.txt:
cmake_minimum_required(VERSION 3.16) project(mylibrary) include(GNUInstallDirs) set(CMAKE_CXX_STANDARD 14) # Register a library - This will created lib[xxx].so add_library(mylibrary SHARED src/library.cpp) configure_file(mylibrary.pc.in mylibrary.pc @ONLY) # List the /include directory target_include_directories(mylibrary PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>) install(TARGETS mylibrary EXPORT mylibraryConfig LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) export(TARGETS mylibrary FILE "${CMAKE_CURRENT_BINARY_DIR}/mylibraryConfig.cmake") install(EXPORT mylibraryConfig DESTINATION "${CMAKE_INSTALL_LIBDIR}/mylibrary/cmake" NAMESPACE mylibraryConfig::) install( DIRECTORY include DESTINATION ${CMAKE_INSTALL_PREFIX}) install(FILES ${CMAKE_BINARY_DIR}/mylibrary.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig) Which I successfully build and install with:
$ cmake .. -DCMAKE_INSTALL_PREFIX=~/libraries/local # Use non-standard destination $ make && make install The executableCMakeLists.txt:
cmake_minimum_required(VERSION 3.16) project(myexecutable) set(CMAKE_CXX_STANDARD 14) find_package(mylibrary REQUIRED) add_executable(myexecutable src/main.cpp) target_link_libraries(myexecutable PUBLIC mylibrary) target_include_directories(myexecutable PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) I can prepare cmake for this project:
$ cmake .. -DCMAKE_PREFIX_PATH=~/libraries/local # Use non-standard location However, building it fails:
$ make fatal error: mylibrary/library.h: No such file or directory 2 | #include <mylibrary/library.h> To my understanding the location of the library (binaries and headers) is embedded in the installed package. And through find_package() that information retrieved, so why isn't it working here?
Similar questions:
- I largely based my library cmake on: How to create a shared library with cmake?
- Same problem but I am already using
target_include_directories: Cmake Linking Shared Library: "No such file or directory" when include a header file from library
find_package()may not succeed and not even report any message about that: https://stackoverflow.com/questions/2711654....cmake ..(without theDCMAKE_PREFIX_PATH) for the program, I get a neat error like "cannot find package mylibrary". So I'm inclined to think that part works.find_package, i.e. usemylibraryConfig::mylibraryin thetarget_link_librariescommand.NAMESPACEbit. If you post this as an answer I'll accept it. Or I'll post it myself later.