cmake引用了以下导入的目标,但是缺少了

时间:2018-04-27 11:42:32

标签: c++ cmake

我有一个包含两个库(liba和libb)的存储库,而liba 取决于libb。它们是单个存储库的一部分,使用单个cmake“上下文”构建。文件结构如下所示:

├── CMakeLists.txt
├── liba
│   ├── CMakeLists.txt
│   ├── internal
│   │   └── private.hh
│   ├── module.cc
│   ├── module.hh
└── libb
    ├── CMakeLists.txt
    ├── other.cc
    └── other.hh

Everything编译和安装没有任何问题。虽然,如果我尝试创建一个依赖于liba的新项目。像这样:

cmake_minimum_required(VERSION 3.5)

find_package(Threads REQUIRED)
find_package(OpenCV REQUIRED)
find_package(liba REQUIRED)

add_executable(exec exec.cc)
target_link_libraries(exec PRIVATE is::liba)

我收到以下错误:

CMake Error at CMakeLists.txt:5 (find_package):
  Found package configuration file:

    /home/hodor/is-sdk/lib/cmake/liba/libaConfig.cmake

  but it set liba_FOUND to FALSE so package "liba" is considered to be NOT
  FOUND.  Reason given by package:

  The following imported targets are referenced, but are missing: is::libb

我在这里缺少什么? Repository link for context

生成liba的CMakeLists:

cmake_minimum_required(VERSION 3.5)
include(GNUInstallDirs)

find_package(OpenCV REQUIRED core)
find_package(Threads REQUIRED)

list(APPEND liba_public_headers 
  "module.hh"
)

list(APPEND liba_private_headers 
  "internal/private.hh"
)

list(APPEND liba_sources 
  "module.cc"
  ${liba_public_headers}
  ${liba_private_headers}
)

add_library(liba ${liba_sources})

target_include_directories(
  liba
 PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}> # for headers when building
  $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> # for generated files in build mode
  $<INSTALL_INTERFACE:include/is/liba> # for clients in install mode
 PRIVATE
  ${OpenCV_INCLUDE_DIRS}
)

target_link_libraries(liba 
 PRIVATE
  is::libb
  ${OpenCV_LIBRARIES} 
  Threads::Threads
)

set_property(TARGET liba PROPERTY CXX_STANDARD 11)

install(
  TARGETS liba EXPORT libaTargets
  LIBRARY  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  ARCHIVE  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  RUNTIME  DESTINATION "${CMAKE_INSTALL_BINDIR}"
  INCLUDES  DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

install(FILES ${liba_public_headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/is/liba)

install(
  EXPORT libaTargets
  FILE libaConfig.cmake
  NAMESPACE is::
  DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/liba
)

生成libb的CMakeLists:

cmake_minimum_required(VERSION 3.5)
include(GNUInstallDirs)

list(APPEND libb_public_headers 
  "other.hh"
)

list(APPEND libb_sources 
  "other.cc"
  ${libb_public_headers}
)

add_library(libb ${libb_sources})

target_include_directories(
  libb
 PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}> # for headers when building
  $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> # for generated files in build mode
  $<INSTALL_INTERFACE:include/is/libb> # for clients in install mode
)

install(
  TARGETS libb EXPORT libbTargets
  LIBRARY  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  ARCHIVE  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  RUNTIME  DESTINATION "${CMAKE_INSTALL_BINDIR}"
  INCLUDES  DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

install(FILES ${libb_public_headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/is/libb)

install(
  EXPORT libbTargets
  FILE libbConfig.cmake
  NAMESPACE is::
  DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libb
)

add_library(is::libb ALIAS libb)

包含liba和libb的顶级CMakeLists:

cmake_minimum_required(VERSION 3.5)

add_subdirectory(liba)
add_subdirectory(libb)

1 个答案:

答案 0 :(得分:5)

看完C++Now 2017: Daniel Pfeifer “Effective CMake" 后,我现在意识到我的配置中缺少了什么。正如@Tsyvarev所提到的,问题是关于libaConfig.cmake。

导出具有依赖项的库时,您应该导出mylibrary Targets.cmake ,如下所示:

install(
  EXPORT libaTargets
  FILE libaTargets.cmake
  NAMESPACE is::
  DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/liba
)

然后手动(有点失望,我不得不说)写一个mylibrary Config.cmake 包含所有依赖项,如下所示:

include(CMakeFindDependencyMacro)

find_dependency(libb)
find_dependency(OpenCV)
find_dependency(Threads)

include("${CMAKE_CURRENT_LIST_DIR}/libaTargets.cmake")