【发布时间】:2020-07-25 23:52:48
【问题描述】:
对于我的研究项目,我正在建立一个项目 (coom) 来对数据结构上的一组算法进行基准测试。对于单元测试,我选择了 Bandit,这让我的项目结构如下所示:
+ root
|-- CMakeLists.txt
|-+ external/
| \-- bandit/
|-+ src/
| |-- CMakeLists.txt
| |-- node.cpp
| \-- node.h
\-+ test/
|-- CMakeLists.txt
|-- test.cpp
\-- test_node.cpp
根据我使用其他语言的经验,这在我看来是一个标准的项目结构? test/ 文件夹包含对 src/ 中逻辑的单元测试,并且没有与源代码和测试代码混合的依赖项,而是在 external/ 中。
我想要如下所示的测试文件(不相关的部分已删除)
// test/test.cpp
#include <bandit/bandit.h>
(...)
#include "test_node.cpp"
int main(int argc, char* argv[]) {
(...)
}
// test/test_node.cpp
#include <coom/node.h>
(...)
但我的问题是,当我尝试使用cmake .. 和随后的Makefile 进行编译时,他们无法在src/ 中找到源代码,我得到编译器错误:
fatal error: coom/node.h: No such file or directory.
我希望test/CMakeLists.txt 看起来应该如下所示:
# test/CMakeLists.txt
add_executable (test_unit test.cpp)
target_link_libraries(test_unit coom)
我不知道如何设置CMakeLists.txt 和src/CMakeLists.txt 以确保获得上述预期结果。目前它们看起来如下:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project (coom VERSION 0.1)
# ============================================================================ #
# Dependencies
(...)
# ============================================================================ #
# COOM project
add_subdirectory (src)
add_subdirectory (test)
# src/CMakeLists.txt
# ============================================================================ #
# Link up files for the library
set(HEADERS
node.h
)
set(SOURCES
node.cpp
)
add_library(coom ${HEADERS} ${SOURCES})
我可以从其他项目中看到,可以将src/ 目录与一些libname/ 前缀链接,但我无法从他们的CMakeLists.txt 文件中辨别出我做错了什么。我已经研究过编写coom.pc.in 文件并提供install-target,并尝试使用set_target_properties 或FOLDER coom 或PREFIX coom,但均未成功。我可以将include_directory(../src) 破解到test/CMakeLists.txt 中,以便能够通过#include <node.cpp> 包含该文件,但这表明我做的事情本质上是错误的。
在这一点上,我非常担心,CMake 文档对我帮助不大。
【问题讨论】:
标签: c++ cmake linker include-path