CMake:如何向已安装的配置添加功能? [英] CMake: How do I add a function to an installed config?
问题描述
我正在创建一个库,我正在使用 CMake 构建和安装该库.在 CMakeLists.txt
中是 install(TARGETS mylib ...)
来安装库本身和 install(EXPORT ...)
来创建一个 CMake 配置.CMake 配置意味着,想要使用自己的 CMakeLists.txt
中的库的应用程序可以通过 find_package()
找到该库.到目前为止,没有什么令人惊讶的.
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.
但除此之外,我还有 useful_fn.cmake
,它包含一个有用的 CMake 函数,我想让应用程序的 CMakeLists.txt
可以使用它.我可以使用 install install(FILE used_fn.cmake)
手动安装它,但是应用程序如何知道在哪里可以找到它?可以从配置中引用吗?
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?
更好的是,CMake 配置可以直接包含已安装的版本吗?所以仅仅运行 find_package(mylib)
就可以访问这个 CMake 函数?如果我手动编写整个 mylib-config.cmake,我可以做到这一点,而不是让 CMake 像当前那样生成它,但我真的宁愿不这样做,只是为了添加一行(包括(.../usefulfn.cmake)
).
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)
).
推荐答案
CMake 应该生成 XXXConfig.cmake
脚本是一种误解.相反,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
和install(EXPORT)
XXXConfigVersion.cmake
和write_basic_package_version_file()
XXXConfigTargets.cmake
withinstall(EXPORT)
XXXConfigVersion.cmake
withwrite_basic_package_version_file()
并且这些脚本包含在 XXXConfig.cmake
脚本中手工编写,您可以在其中定义其他内容:
and these scripts are included in the XXXConfig.cmake
script written by hands, where you may define additional things:
# File: XXXConfig.cmake
include(${CMAKE_CURRENT_LIST_DIR}/XXXConfigVersion.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/XXXConfigTargets.cmake)
# Here you may provide additional functions and other things.
function(my_func)
...
endfunction()
这篇关于CMake:如何向已安装的配置添加功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!