【发布时间】:2020-06-26 17:27:33
【问题描述】:
我的存储库中有多个项目(子目录)。所有项目只有一个名为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 等其他问题无法解决我的问题,因为它们通常适用于更复杂的文件夹结构。还有一些问题试图仅针对 this one 这样的单个项目,但我有多个项目。
你能提供一个简洁的解决方案吗?
【问题讨论】: