使所有项目(子目录)都可以访问文件夹中的所有文件

Make all files in a folder accessible to all projects (subdirectories)

我的存储库中有多个项目(子目录)。所有项目只有一个名为 main.cpp 的可执行文件,它们都使用 common 文件夹中的库和 #include 语句。文件夹结构如下所示:

root
|
├────common
|    ├──── example.h
|    ├──── example.cpp
|    ├──── *.h          # many header files
|    └──── *.cpp        # many source files
|
├────Project_A
|    ├──── CMakeLists.txt
|    └──── main.cpp
|
├────Project_B
|    ├──── CMakeLists.txt
|    └──── main.cpp
|
└──── CMakeLists.txt

这是我尝试编写 root 的 CMakeLists.txt

cmake_minimum_required (VERSION 3.8)
project ("root")

file(GLOB_RECURSE CommonLibs ${CMAKE_SOURCE_DIR}/common/*.cpp)
link_libraries(${CommonLibs})

add_subdirectory ("Project_A")
add_subdirectory ("Project_B")

Project_A的CMakeLists.txt

cmake_minimum_required (VERSION 3.8)
add_executable (Project_A "main.cpp")

Project_B的CMakeLists.txt

cmake_minimum_required (VERSION 3.8)
add_executable (Project_B "main.cpp")

但是,当 运行 任何项目出现此错误时:

LNK1107 invalid or corrupt file: cannot read at 0x7AC, file example.h

我不认为文件已损坏,因为在我尝试在 root 的 CMakeLists.txt 中使用 link_libraries() 之前,我遇到了其他错误:

Unresolved external symbol SomeNamespace::ExampleClass::ExampleClass(bool)

可能重复

其他问题如this one don't solve my problem because they usually work with a more complex folder structure. Also there are questions that attempt to target only single project like this one,但我有多个项目

能否请您提供一个简洁的解决方案?

CMakeLists.txt 添加到您的 common/ 目录:

root
|
├────common
|    ├──── CMakeLists.txt <-------- Add this
|    ├──── example.h
....

common/CMakeLists.txt

file(GLOB_RECURSE CommonLibsFiles ${CMAKE_CURRENT_SRC_DIR}/*.cpp)
add_library(CommonLibs ${CommonLibsFiles})
...

root 的 CMakeLists.txt

...

### NO NEED FOR THESE
file(GLOB_RECURSE CommonLibs ${CMAKE_SOURCE_DIR}/common/*.cpp)
link_libraries(${CommonLibs})
###

add_subdirectory(common) #CommonLibs will now be visible to children directories
...

现在 link 需要的库。例如 项目 A:

Project_A的CMakeLists.txt

cmake_minimum_required (VERSION 3.8)
add_executable (Project_A "main.cpp")
target_link_libraries(Project_A PRIVATE CommonLibs) #link to common libs here,

所有 targets/variables 创建的或对 parent cmake 文件可见的,对 children 可见。但事实并非如此。为了 children 将其变量公开给 parent,他们需要明确指定 PARENT_SCOPE

顶级 CMake 中的以下几行没有多大意义:

file(GLOB_RECURSE CommonLibs ${CMAKE_SOURCE_DIR}/common/*.cpp)
link_libraries(${CommonLibs})

link_libraries()命令接受库目标,而不是变量(例如CommonLibs)。您可以使用 CommonLibs 变量定义一个新的库目标,然后link 库目标 到您的可执行文件使用link_libraries():

file(GLOB_RECURSE CommonLibs ${CMAKE_SOURCE_DIR}/common/*.cpp)
# Add this line.
add_library(MyCommonLib SHARED ${CommonLibs})
link_libraries(MyCommonLib)

请注意 link_libraries() 通常 不鼓励 使用,即使在 CMake 文档本身中也是如此。您应该更喜欢 linking 您的 MyCommonLib 而不是需要它的 特定 可执行文件 target_link_libraries.

target_link_libraries(Project_A PRIVATE MyCommonLib)