我有静态库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)
Run Code Online (Sandbox Code Playgroud)
相关章节来自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)
Run Code Online (Sandbox Code Playgroud)
最后Baz CmakeLists.txt:
find_package(Bar REQUIRED)
target_link_libraries(Baz PRIVATE Bar::Bar)
Run Code Online (Sandbox Code Playgroud)
现在在构建时,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?
Run Code Online (Sandbox Code Playgroud)
因此,build查找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)
Run Code Online (Sandbox Code Playgroud)
然后我CMake/BarConfig.cmake用这个创建了一个文件:
include("${CMAKE_CURRENT_LIST_DIR}/BarTargets.cmake")
find_package(Foo REQUIRED)
Run Code Online (Sandbox Code Playgroud)
现在,这BarConfig.cmake将在全球范围内安装并find_package进行查找Foo。
| 归档时间: |
|
| 查看次数: |
709 次 |
| 最近记录: |