首页 > 解决方案 > CMakeLists 安装没有依赖头文件的库

问题描述

我正在尝试使用 cmake 的安装/导出设置我的库的构建过程,为此构建的 lib 和头文件将使用 .deb 打包为 .deb dpkg-buildpackage。我大部分时间都在尝试遵循https://cmake.org/cmake/help/git-stage/guide/importing-exporting/index.html上的指南,以便在安装后使用find_package.

我面临的问题是当我的库具有来自其他文件夹/项目的嵌套依赖项时,我使用add_subdirectory().

我要构建/导出的项目的主要 CMake 是

cmake_minimum_required(VERSION 3.10)
project(exampleLib)

add_subdirectory(../path/to/dependencyA ${PROJECT_BINARY_DIR}/dependencyA)

include(GNUInstallDirs)

add_library(exampleLib SHARED
    exampleLib.cpp
)

target_link_libraries(exampleLib PRIVATE
    dependencyA
)

target_include_directories(dashStreamerClient PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

# dependencyA has a dependency on "dependencyB"
# which if I don't include here, errors with 'CMake Error: install(EXPORT "exampleLibTargets" ...) includes target "dependencyA" which requires target "dependencyB" that is not in any export set.'
install(TARGETS exampleLib dependencyA dependencyB
        EXPORT exampleLibTargets
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
        INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

# Note: I only want this exampleLib.h file to be "installed" in CMAKE_INSTALL_INCLUDEDIR, not any of the other dependencies headers as they are unnecessary!
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/exampleLib.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT exampleLibTargets
        FILE exampleLibTargets.cmake
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/exampleLib
)
# Make a package configuration file
include(CMakePackageConfigHelpers)
# Config.cmake.in copied from the importing-exporting cmake guide
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in
  "${CMAKE_CURRENT_BINARY_DIR}/exampleLibConfig.cmake"
  INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/exampleLib
)
install(FILES
          "${CMAKE_CURRENT_BINARY_DIR}/exampleLibConfig.cmake"
          "${CMAKE_CURRENT_BINARY_DIR}/exampleLibConfigVersion.cmake"
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/exampleLib
)
export(EXPORT exampleLibTargets
       FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/exampleLibTargets.cmake"
)

而我的dependencyA的CMakeLists.txt是

cmake_minimum_required(VERSION 3.10)
project(dependencyA)

add_subdirectory(path/to/dependencyB ${PROJECT_BINARY_DIR}/dependencyB)

add_library(dependencyA SHARED
    dependencyA.cpp
)

target_link_libraries(dependencyA PUBLIC
    dependencyB
)

target_include_directories(dependencyA PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)

问题是当我尝试时make install,我得到了错误

CMake Error at cmake_install.cmake:152 (file):
  file INSTALL cannot find
  "/home/me/path/to/exampleLib/include/dependencyB.h":
  No such file or directory.

其中 CMake 试图在我当前的 CMake 项目的路径中找到嵌套依赖项B 的标头,但dependencyB.h实际上是在path/to/dependencyB依赖项 A 的 add_subdirectory 中定义的 eg 中。我实际上也不希望dependencyB.h安装它,因为它不是必需的,只需要exampleLib.h.

我究竟做错了什么?

谢谢!

标签: c++cmake

解决方案


推荐阅读