How to determine the exact lib/include variable generated by find_packge() in config mode? [duplicate]

Solution 1:

The usage of results of find_package(XXX) depends on the script which is actually used by this command:

  1. FindXXX.cmake.

    This file is either shipped with the project which wants to use it, or with CMake. Usually, such "Find" scripts start with some description about their usage.

  2. XXXConfig.cmake or xxx-config.cmake.

    "Config" scripts are usually installed with the package. Normally, they are generated by CMake using specific CMake commands. You need to consult the package's documentation for use such scripts properly.

If the package doesn't document the usage of its "Config" script, then you may only guess about its usage.

Reading a "Config" script itself could be difficult: CMake includes into it a lot of "sugar", which helps portability and compatibility.

Normally, a "Config" script provides some IMPORTED library target(s), which can be used with target_link_libraries call.

One may scan the project's CMakeLists.txt for a line like

install(TARGETS <targets...> EXPORT ...)

Here <targets...> denotes targets, which can be accessed after find_package() call.

Also scan for a line like

install(EXPORT ...)

if it has NAMESPACE option, then its value prepends all exported targets.

Example:

For docopt package, its CMakeLists.txt contains a line

install(TARGETS docopt EXPORT ${export_name} DESTINATION ${CMAKE_INSTALL_LIBDIR})

thus its docopt target is exported.

As installation of the export file

install(EXPORT ${export_name} DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/docopt")

doesn't contain NAMESPACE option, the export target is accessible with its original name:

find_package(docopt REQUIRED)
...
add_executable(${PROJECT_NAME} main.cpp)
# Use *IMPORTED* 'docopt' target
target_link_libraries(${PROJECT_NAME} docopt)