所以我试图了解我的一个C ++项目出了什么问题。基本上,项目1工作正常,一切都很好。在项目的主头文件中我有
#include "spdlog/spdlog.h"
我将spdlog作为项目1中的子项目。此外,在项目1的CMake中,我有include_directories(spdlog/include)
。现在,我正在构建项目2,它依赖于项目1并将其作为子项目。但是,当我尝试添加spdlog
时,它不允许我,并希望我制作完整的../project1/spdlog/include/spdlog.h
。组织此依赖关系并包含标头的正确方法是什么?
答案 0 :(得分:1)
您应该寻找现代的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)
项目2 /的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开头的演示文稿)