因此,我试图了解我的一个C++项目出了什么问题。本质上,项目1正常,一切都很好。在项目的主头文件中,我有

#include "spdlog/spdlog.h"

我在项目1中将spdlog作为子项目。此外,在项目1的CMake中,我具有include_directories(spdlog/include)。现在,我正在构建项目2,该项目取决于项目1,并将其作为子项目。但是,当我尝试包含spdlog时,它不允许我并要我制作完整的../project1/spdlog/include/spdlog.h。组织此依赖性并包含 header 的正确方法是什么?

最佳答案

您应该寻找现代的cmake资源。在现代cmake样式中,您可以为spdlog创建一个Imported目标,然后可以在其他任何地方使用它。

让我们假设以下结构

external/spdlog
external/CMakeLists.txt
project1/CMakeLists.txt
project2/CMakeLists.txt
CMakeLists.txt

在你写的external/CMakeLists.txt
## add the imported library which is
##   an INTERFACE (ie header only)
##   IMPORTED (ie third party - no compilation necessary)
##   GLOBAL (you can reference it from everywhere
##           even if the target is not in the same scope
##           which is the case for project1 and project2 because
##           the spdlog  target is not in their parent folder)
add_library(spdlog INTERFACE IMPORTED GLOBAL)
## set the include directory which is to be associated with this target
## and which will be inherited by all targets which use
## target_link_libraries(<target> spdlog)
target_include_directories(spdlog INTERFACE spdlog/include)

在根CMakeLists.txt中,您编写解决方案配置(示例)
project(mysolution)
add_subdirectory(external)
add_subdirectory(project1)
add_subdirectory(project2)

在项目CMakeLists.txt中,您现在可以使用在external中创建的目标

project1/CMakeLists.txt
add_library(project1 ${Sources})
## this will set the include directories configured in the spdlog target (library)
## and also ensure that targets depending on target1 will have access to spdlog
## (if PUBLIC were changed to PRIVATE then you could only use spdlog in
## project1 and dependeing targets like project2 would not
## inherit the include directories from spdlog it wouldn't
target_link_libraries(project1 PUBLIC spdlog)

project2/CMakeLists.txt
add_library(project2 ${Sources})
## this will link project1 which in return also links spdlog
## so that now project2 also has the include directories (because it was inherited)
## also targets depending on project2 will inherit everything from project1
## and spdlog because of the PUBLIC option
target_link_libraries(project2 PUBLIC project1)

资料来源:

[0] https://cmake.org/cmake/help/v3.9/command/add_library.html

[1] https://cmake.org/cmake/help/v3.9/command/target_link_libraries.html?highlight=target_link_libraries

[2] http://thetoeb.de/2016/08/30/modern-cmake-presentation/(请参阅从幻灯片20开始的演示文稿)

10-08 12:49