我正在尝试使用现代CMake(无全局变量和目标)编写构建脚本。到目前为止一切都很好,但是现在我想将一个库(模块2)链接到依赖第一个库(模块1)。但是,编译器找不到module2的公共头,但是include目录在“ build / CMakeFiles / Module1.dir / includes_CXX.rsp”中列出。所以我的问题是,我该如何工作?
项目的结构:
|-Module1
|-|-inc (public headers)
|-|-src (private headers and sources)
|-|-CMakeLists.txt (3)
|-Module2
|-|-inc (here is "module2.h" which I want to include in Module1)
|-|-src
|-|-CMakeLists.txt (2)
|CMakeLists.txt (1)
Project-CMakeLists.txt(1):目前没有可执行文件
cmake_minimum_required(VERSION 3.8)
project(foo CXX)
add_subdirectory(Module2)
add_subdirectory(Module1)
Module2-CMakeLists.txt(2):
add_library(Module2 OBJECT
${CMAKE_CURRENT_SOURCE_DIR}/src/module2_1.cpp
)
target_include_directories(Module2
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
)
Module1-CMakeLists.txt(3):
add_library(Module1 OBJECT
${CMAKE_CURRENT_SOURCE_DIR}/src/add.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/sub.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/functions2mock.cpp
)
target_include_directories(Module1
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
)
target_link_libraries(Module1 PRIVATE Module2)
我认为使用命令“ target_link_libraries”,Module1从Module2获取所有公共包含,标志等。
我希望有人能帮助我,谢谢。
问候。