Creating and Managing Config-Files
Let's learn about safely generating and installing target export files and writing basic config-files.
Writing basic config-files
A complete package definition consists of the target export files, the package's config-file, and the package's version file, but technically, all that's needed for find_package()
to work is a config-file. It's considered a package definition, and it's responsible for providing any package functions and macros, checking requirements, finding dependencies, and including target export files.
As we mentioned earlier, users can install our package anywhere on their system by using the following command:
cmake --install <build tree> --prefix=<installation path>
This prefix determines where the installed files will be copied. To support this, we must at least ensure the following:
The paths on the target properties can be relocated.
The paths that are used in our config-file are relative to it.
To use such packages that have been installed in non-default locations, the consuming projects need to provide <installation path>
through the CMAKE_PREFIX_PATH
variable during the configuration stage. We can do this with the following command:
cmake -B <build tree> -DCMAKE_PREFIX_PATH=<installation path>
The find_package()
command will scan the list of paths that are outlined in the documentation in a platform-specific way. One of the patterns that's checked on Windows and Unix-like systems is as follows:
<prefix>/<name>*/(lib/<arch>|lib*|share)/<name>*/(cmake|CMake)
This tells us that installing the config-file in a path such as lib/calc/cmake
should work just fine. Also, it's important to highlight that config-files must be named <PackageName>-config.cmake
or <PackageName>Config.cmake
to be found.
Let's add the installation of the config-file to the 06-install-export
example:
...install(EXPORT CalcTargetsDESTINATION ${CMAKE_INSTALL_LIBDIR}/calc/cmakeNAMESPACE Calc::)install(FILES "CalcConfig.cmake"DESTINATION ${CMAKE_INSTALL_LIBDIR}/calc/cmake)
This command will install CalcConfig.cmake
from the same ...