c++ - 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
.
我究竟做错了什么?
谢谢!
解决方案
推荐阅读
- ruby - 在 48 颗宝石 (Gem::MissingSpecError) 中找不到 'cocoapods' (>= 0.a)
- .htaccess - .htaccess - 在 URL 中隐藏 subfolder1 但显示 subfolder2(其他网站)
- smartcontracts - Masterchef合约添加功能
- python - Python字符串切片结果在不同切片索引处不可预测或相同
- sql - Postgresql:如何删除 table_1 中与 table_2 不匹配的条目
- c# - 在.net core web api中异步调用长时间运行进程的最佳方法
- keras - 删除keras模型中的前n层
- javascript - 当前请求不是 Angular 11 中的多部分请求
- python - 基于先前变量的新列
- mongodb - mongodb:在 ifNull 之后使用内部属性