CMake:如何将功能添加到已安装的配置中? [英] CMake: How do I add a function to an installed config?
问题描述
我正在创建一个库,该库是使用CMake构建和安装的。在 CMakeLists.txt
中是 install(TARGETS mylib ...)
来安装库本身和 install(EXPORT ...)
创建CMake配置。 CMake配置意味着希望使用 find_package()
的应用程序可以从自己的 CMakeLists.txt $ c中使用该库。 $ c>。到目前为止,不足为奇。
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 valid_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生成它,我可以这样做,但是我真的不愿意这样做,只是为了添加一行( include(... / 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包装文档 a>。
这篇关于CMake:如何将功能添加到已安装的配置中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!