安装CMake库:是否还为依赖项提供了查找模块? [英] Installing a CMake library: also ship find modules for the dependencies?
问题描述
我的CMake库 MyLibrary
具有 OtherLibrary
的依赖关系,我使用非-standard FindOtherLibrary.cmake
。
My CMake library, MyLibrary
, has a dependency for OtherLibrary
, that I import with a non-standard FindOtherLibrary.cmake
.
我的库依赖于 OtherLibrary
公开:
target_link_libraries(MyLibrary PUBLIC OtherLibrary::OtherLibrary)
当我安装 MyLibrary
时(连同 MyLibraryConfig.cmake
),并且用户希望链接到该链接,因此需要导入 OtherLibrary
。
When I install MyLibrary
(together with MyLibraryConfig.cmake
), and users want to link against it, they therefore need to import OtherLibrary
.
关于如何在 MyLibrary中分发
FindOtherLibrary.cmake
有很好的做法 code>?
Is there a good practice regarding how to distribute FindOtherLibrary.cmake
along MyLibrary
?
理想情况下,对于 MyLibrary的用户来说,这可能会让事情变得更加轻松
,方法是从安装的配置文件 MyLibraryConfig.cmake
中自动导入 OtherLibrary
像
Ideally, one could make things even easier for users of MyLibrary
by importing OtherLibrary
automatically from the installed config file MyLibraryConfig.cmake
, if it contains something like
include(CMakeFindDependencyMacro)
find_dependency(OtherLibrary)
并知道 FindOtherLibrary.cmake
在哪里。
这有可能吗?
推荐答案
我最终找到了解决问题的方法。
I ended up finding a solution to my question.
原则上,它会执行@utopia的建议,但会以自动化的方式进行:我图书馆的最终用户不需要设置(甚至不知道) FindOtherLibrary。 cmake
。它将由 MyLibraryConfig.cmake
自动导入。
In principle, it does what @utopia suggested, but in an automated fashion: the end user of my library does not need to set up (or even know about) FindOtherLibrary.cmake
. It will be imported automatically by MyLibraryConfig.cmake
.
为此,我安装了 FindOtherLibrary.cmake
和 MyLibraryConfig.cmake
:
install(FILES
/path/to/MyLibraryConfig.cmake
DESTINATION
lib/cmake/MyLibrary
)
install(FILES
/path/to/FindOtherLibrary.cmake
DESTINATION
lib/cmake/MyLibrary/Modules
)
然后在 MyLibraryConfig.cmake
中设置如何导入它:
And in MyLibraryConfig.cmake
I set up how to import it:
include(CMakeFindDependencyMacro)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/Modules/")
find_dependency(OtherLibrary REQUIRED)
请注意,我设置了变量 CMAKE_MODULE_PATH
,因为无法指定查找模块的位置在 find_package
或 find_de中待命
(仅适用于配置模式)。
Note that I set the variable CMAKE_MODULE_PATH
because it is not possible to specify the location of find modules in find_package
or find_dependency
(works only for config mode).
这篇关于安装CMake库:是否还为依赖项提供了查找模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!