gpt4 book ai didi

通过 find_package() 找不到 CMake 传递依赖

转载 作者:行者123 更新时间:2023-12-04 14:39:18 24 4
gpt4 key购买 nike

我有静态库 Foo , 静态库 Bar这取决于 Foo和可执行 Baz这取决于 Bar .

相关栏目来自 Foo CMakeLists.txt :

# Specifying files to copy during "make install" command.
install(TARGETS Foo EXPORT FooConfig
INCLUDES DESTINATION include
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)

# Specifying config file that will be used to find a library using find_package().
install(EXPORT FooConfig
FILE FooConfig.cmake
NAMESPACE Foo::
DESTINATION lib/cmake/Foo)
export(TARGETS Foo
NAMESPACE Foo::
FILE FooConfig.cmake)

相关栏目来自 Bar CMakeLists.txt :
# Specifying libraries that are required for build.
find_package(Foo REQUIRED)

# Specifying libraries to link to for the users of the library.
target_link_libraries(Bar PUBLIC Foo::Foo)

# Specifying files to copy during "make install" command.
install(TARGETS Bar EXPORT BarConfig
INCLUDES DESTINATION include
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)

# Specifying config file that will be used to find a library using find_package().
install(EXPORT BarConfig
FILE BarConfig.cmake
NAMESPACE Bar::
DESTINATION lib/cmake/Bar)
export(TARGETS Bar
NAMESPACE Bar::
FILE BarConfig.cmake)

最后 Baz CmakeLists.txt :
find_package(Bar REQUIRED)

target_link_libraries(Baz PRIVATE Bar::Bar)

现在正在 build Baz我得到:
CMake Error at CMakeLists.txt:19 (add_executable):
Target "Baz" links to target "Foo::Foo" but the
target was not found. Perhaps a find_package() call is missing for an
IMPORTED target, or an ALIAS target is missing?

所以构建发现 Bar并正确确定它依赖于 Foo但找不到 Foo .我有另一个直接依赖于 Foo 的测试项目它构建得很好。如何解决这个问题?

最佳答案

不幸的是,BarConfig.cmake像这样生成的不处理查找依赖项,我不得不修改 Bar CMakeLists.txt对此:

# Specifying files to copy during "make install" command.
install(TARGETS Bar EXPORT BarTargets
INCLUDES DESTINATION include
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
install(FILES CMake/BarConfig.cmake DESTINATION lib/cmake/Bar)

# Specifying config file that will be used to find a library using find_package().
install(EXPORT BarTargets
FILE BarTargets.cmake
NAMESPACE Bar::
DESTINATION lib/cmake/Bar)
export(TARGETS Bar
NAMESPACE Bar::
FILE BarTargets.cmake)

然后我创建了一个文件 CMake/BarConfig.cmake有了这个:
include("${CMAKE_CURRENT_LIST_DIR}/BarTargets.cmake")

find_package(Foo REQUIRED)

现在这个 BarConfig.cmake在全局安装并调用 find_package找到 Foo .

关于通过 find_package() 找不到 CMake 传递依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46861504/

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