4
votes

So I have two CMake projects, MyLib and MyApp. I build, install and export MyLib to a custom directory (right now just using my desktop: /Users/MyName/Desktop/InstallDir) and then I want to use find_package() to find MyLin when building MyApp.

So my CMakeLists.txt file for MyLib has the following code:

set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/../../install")

target_include_directories(mylib PUBLIC 
                      $<INSTALL_INTERFACE:include> )

install(TARGETS mylib
        EXPORT mylib-targets 
        PUBLIC_HEADER DESTINATION include 
        ARCHIVE DESTINATION lib
        LIBRARY DESTINATION lib
        RUNTIME DESTINATION bin)

install(EXPORT mylib-targets 
        NAMESPACE mylib::
        FILE mylib-config.cmake
        DESTINATION lib/cmake/mylib)

export(PACKAGE mylib)

This creates a mylib-config.cmake file in install/cmake/mylib

Then in MyApp's CMakeLists.txt file I have the following code

set(mylib_DIR "${CMAKE_SOURCE_DIR}/../../install/lib/cmake/mylib")
set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/../../install")

find_package(mylib REQUIRED)
message("Include dirs: "  ${mylib_INCLUDE_DIRS} )
message("Libs: " ${mylib_LIBRARIES} )

However, when I build MyApp, mylib_INCLUDE_DIRS and mylib_LIBRARIES are both empty. If I check the mylib-config.cmake file, those variables are not defined in there either, but I thought that when exporting a package, those were automatically generated?

What steps am I missing?

1

1 Answers

2
votes

Export file, generated by CMake, defines IMPORTED targets, not a variables. Exactly for those targets you set NAMESPACE option in the install(EXPORT) command.

Variables like XXX_INCLUDE_DIRS and XXX_LIBRARIES are set by some FindXXX.cmake scripts (old style). Also, these variables can be set in XXXConfig.cmake script, if it is written manually (that script can include the script generated by CMake, and can wrap IMPORTED targets with variables).