- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这可能是一个 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)
是可能的,但 可选:可以直接使用 foo
或 foo::foo
目标,而无需任何 find_package()
。
关于c++ - 如何在从顶级 add_subdirectory 添加的包上使用 find_package?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55870281/
我是一名优秀的程序员,十分优秀!