1

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:

4
  • As far as I know find_package() may not succeed and not even report any message about that: https://stackoverflow.com/questions/2711654.... Commented Jan 6, 2021 at 16:06
  • If I run cmake .. (without the DCMAKE_PREFIX_PATH) for the program, I get a neat error like "cannot find package mylibrary". So I'm inclined to think that part works. Commented Jan 6, 2021 at 16:58
  • As you namespaced your shared library target in the config file you need to reference it with the full name when using find_package, i.e. use mylibraryConfig::mylibrary in the target_link_libraries command. Commented Jan 6, 2021 at 23:02
  • Yup, that's it! I just removed the NAMESPACE bit. If you post this as an answer I'll accept it. Or I'll post it myself later. Commented Jan 7, 2021 at 7:09

1 Answer 1

1

When a shared library target is namespaced in the config file you need to reference it with the full name in the downstream packages when using find_package, i.e. you need to use

target_link_libraries(myexecutable PUBLIC mylibraryConfig::mylibrary) 

Alternatively, remove the namespace from the install by replacing

install(EXPORT mylibraryConfig DESTINATION "${CMAKE_INSTALL_LIBDIR}/mylibrary/cmake" NAMESPACE mylibraryConfig::) 

...with:

install(EXPORT mylibraryConfig DESTINATION "${CMAKE_INSTALL_LIBDIR}/mylibrary/cmake") 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.