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

时间:2017-10-21 08:35:59

标签: cmake

我有静态库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)

现在,在构建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的测试项目,它构建得很好。如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

不幸的是,像这样生成的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的{​​{1}}。