gpt4 book ai didi

c++ - 如何在从顶级 add_subdirectory 添加的包上使用 find_package?

转载 作者:行者123 更新时间:2023-11-30 04:48:21 26 4
gpt4 key购买 nike

这可能是一个 xy 问题,所以这是我的情况。

背景


我有以下项目结构:

-project
-examples
-example_that_uses_mylib_1
* CMakeLists.txt
* main.cpp
-example_that_uses_mylib_2
* CMakeLists.txt
* main.cpp
-external
-notmylib_a
* CMakeLists.txt
* ... (other stuff)
-notmylib_b
* CMakeLists.txt
* ... (other stuff)
-src
-mylib_stuff
* file1.cpp
* file1.h
*CMakeLists.txt
CMakeLists.txt

我正在尝试制作一个执行以下操作的 cmake 文件:

  • 允许 mylib 依赖于在 external 中找到的第 3 方库中的目标,而不使用 add_subdirectory,因为它们不是实际上是子目录,这是不好的做法。

  • 允许 example_that_uses_mylib 可执行文件依赖于 mylib 目标而不将其添加为子目录。

在看到 this project 之前,我不确定如何执行此操作谁的顶级 cmake 这样做:

add_subdirectory(lib/foo)
add_subdirectory(src/bar)
add_subdirectory(src/baz)

bar 和 baz CMakeLists.txt 这样做:

#Bar
find_package(foo 0.1.2 CONFIG REQUIRED)

#Baz
find_package(bar CONFIG REQUIRED)

这让我觉得我可以对我的图书馆做同样的事情。我做不到。

最初单个顶级 CMakeLists.txt 构建了所有目标,我想摆脱这一点,并使用 add_subdirectories 拆分构建,从 mylib 开始。

问题


最初我的顶级 CMakeLists.txt 看起来很像这样(以前有效):

add_subdirectory(external/notmylib_a)
add_library(mylib STATIC src/mylib_stuff/file1.cpp)
target_include_directories(mylib PUBLIC src/)
target_link_libraries(mylib PRIVATE notmylib_a::notmylib_a)

当我决定将事情分开时,我最初是这样做的(这也有效):

#CMakeLists.txt
add_subdirectory(external/notmylib_a)


#src/CMakeLists.txt
add_library(mylib STATIC src/mylib_stuff/file1.cpp)
target_include_directories(mylib PUBLIC src/)
target_link_libraries(mylib PRIVATE notmylib_a::notmylib_a)

然后为了跟随另一个项目,我决定这样做:

#CMakeLists.txt
add_subdirectory(external/notmylib_a)


#src/CMakeLists.txt
find_package(notmylib_a CONFIG REQUIRED) #NEW LINE!!
add_library(mylib STATIC src/mylib_stuff/file1.cpp)
target_include_directories(mylib PUBLIC src/)
target_link_libraries(mylib PRIVATE notmylib_a::notmylib_a)

我在 CMAKE 中遇到错误

CMake Error at src/CMakeLists.txt:25 (find_package):
Could not find a package configuration file provided by "notmylib_a"
with any of the following names:

notmylib_aConfig.cmake
notmylib_a-config.cmake

Add the installation prefix of "notmylib_a" to CMAKE_PREFIX_PATH or set
"notmylib_a_DIR" to a directory containing one of the above files. If
"notmylib_a" provides a separate development package or SDK, be sure it
has been installed.

其他项目如何以这种方式利用 find_package

最佳答案

How was the other project able to utilize find_package in such a way?

其他 foo 项目的配置文件 has these lines :

if(NOT TARGET foo::foo)
include("${foo_CMAKE_DIR}/foo-targets.cmake")
endif()

也就是说,当 foo 包含在 add_subdirectory 方法中并创建 foo::foo 目标时,find_package(foo) 实际上忽略它的配置文件。

这在 foo's CMakeLists.txt 中有说明:

# We also add an alias definition so that we shadown
# the export namespace when using add_subdirectory() instead.
add_library(foo::foo ALIAS foo)

换句话说,通过 add_subdirectory 方法包含给定的 foo 包,使用 find_package(foo) 是可能的,但 可选:可以直接使用 foofoo::foo 目标,而无需任何 find_package()

关于c++ - 如何在从顶级 add_subdirectory 添加的包上使用 find_package?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55870281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com