I am creating a library which I am building and installing with CMake. In the CMakeLists.txt
is install(TARGETS mylib ...)
to install the library itself and install(EXPORT ...)
to create a CMake config. The CMake config means that the library can be found with find_package()
by applications wanting to use the library from their own CMakeLists.txt
. So far, nothing surprising.
But in addition to that I have useful_fn.cmake
that contains a useful CMake function that I want to make available to the applications' CMakeLists.txt
. I can install it manually with install install(FILE useful_fn.cmake)
, but how will the applications know where to find it? Can it be referenced from the config?
Even better, could the CMake config include the installed version directly? So merely running find_package(mylib)
provides access to this CMake function? I could do this if I wrote my whole mylib-config.cmake by hand, rather than than getting CMake to generate it like it currently does, but I would really rather not do that just so that I can add one line (include(.../usefulfn.cmake)
).
It is misconception that CMake should generate
XXXConfig.cmake
script. As opposite, intended behavior that CMake generates every other script (names can be any):XXXConfigTargets.cmake
withinstall(EXPORT)
XXXConfigVersion.cmake
withwrite_basic_package_version_file()
and these scripts are included in the
XXXConfig.cmake
script written by hands, where you may define additional things:See more in the CMake packaging documentation.
To clarify further, there is a module that helps your configure valid and relocatable config file.
See macro
configure_package_config_file
provided by CMakePackageConfigHelpers module.As mentioned by @Tsyvarev, the
XXXConfig.cmake
file should still be written by hand but configured withconfigure_package_config_file
instead ofconfigure_file
.