'Difference between FooTarget.cmake and FindFoo.cmake?
CMake automatically creates a FooTarget.cmake file by e.g. adding
install(EXPORT FooTargets
FILE FooTargets.cmake
NAMESPACE Foo::
DESTINATION lib/cmake/Foo
)
into the CMakeLists.txt of a library named Foo. What's the difference between FooTargets.cmake and FindFoo.cmake? With the above snippet, FooTarget.cmake is automatically installed by running sudo make install in the build directory.
However, it seems like such a library isn't necessarily found with find_package(Foo). What's the point of this FooTarget.cmake file if it doesn't make the library automatically discoverable? Since the library is generated by CMake it would seem like CMake itself should know how to generate the FindFoo.cmake library that makes the library automatically discoverable by other projects using CMake and put it where it should go on the platform.
Solution 1:[1]
Discovering a package by using find_package(Foo) consists in two steps:
Using predefined algorithm,
find_packagelocates a scriptFooConfig.cmakedescribing the packageFoo.The script
FooConfig.cmakeis processed by CMake in the context of the main project (which callsfind_package).
This script sets variables and/or IMPORTED targets, which can be used further by the main project.
The script FooTarget.cmake generated by CMake helps in the second step. This script is intended to be included (with include()) into FooConfig.cmake. When FooTarget.cmake is processed by CMake, it creates IMPORTED targets and sets their properties.
So, the very basic FooConfig.cmake script contains just a single line:
include("${CMAKE_CURRENT_LIST_DIR}/FooTarget.cmake")
and installed using install(FILES) into the directory where find_package could locate the script (a directory lib/cmake/Foo perfectly fits for this purpose).
Such simple content of the FooConfig.cmake script implies it to be installed near the FooTarget.cmake script included into it.
More complex FooConfig.cmake script could process COMPONENTS request of find_package, define functions/macros usable by the main project and so on. Examples of that script could be found in CMake documentation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
