c++ - 如何从其他目录树将源文件包含到 Bool.Test 用例中?

标签 c++ unit-testing boost cmake

这在某种程度上是我上一个问题的后续问题:How to achieve organisation of test suites and cases with Boost?

目录结构如另一个问题中所述,我也遵循其中描述的 Boost.Test 用例组织。

但是,我正在努力让一切都与 CMake 一起工作。

我的 tests/CMakeLists.txt如下:

# tests/CMakeLists.txt
# Add the test sources
set(test_SOURCES ${PROJECT_SOURCE_DIR}/tests/TestRunner.cpp)

# Add test cases
add_subdirectory(model)

# Include Boost and program's sources (find_package has been executed before)
include_directories(${Boost_INCLUDE_DIR})

# Create the tests
add_executable(MyProgramTests ${test_SOURCES})

# Link agains Boost libraries
target_link_libraries(MyProgramTests ${Boost_LIBRARIES})

tests/model/CMakeLists.txt说:

# tests/model/CMakeLists.txt
include(${PROJECT_SOURCE_DIR}/src/model/CMakeLists.txt)

# Add test cases to the list
set(test_SOURCES
  ${test_SOURCES}
  ${model_SOURCES}
  ${PROJECT_SOURCE_DIR}/tests/model/model_a_test.cpp
  PARENT_SCOPE
)

src/model/CMakeLists.txt我刚刚写了:

set(model_SOURCES ${PROJECT_SOURCE_DIR}/src/model/model_a.cpp)

我如何必须包含 src/model/model_a.cpp在我的 tests/model/model_a_test.cpp所以编译终于找到了吗?我试过 #include "src/model/model_a.cpp" , #include "model/model_a.cpp"甚至只是#include "model_a.cpp" .在所有情况下 model_a.cpp找不到。

我很确定,我在 CMakeLists 文件中遗漏了一些东西。但是什么?

编辑

我终于通过添加程序源目录解决了${PROJECT_SOURCE_DIR}/src/modelinclude_directories命令 tests/CMakeLists.txt .有了这个,#include "model_a.cpp"应该使用。

最佳答案

看起来您缺少 PARENT_SCOPE 以便将 ${model_SOURCES} 的值从 src/model/CMakeLists.txttests/model/CMakeLists.txt

尝试使用

set(model_SOURCES ${PROJECT_SOURCE_DIR}/src/model/model_a.cpp PARENT_SCOPE)

关于c++ - 如何从其他目录树将源文件包含到 Bool.Test 用例中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9344487/

相关文章:

python - 如何安装 MultiNEAT

c++ - spirit X3 : Rule that parses a single character and generates a string

c++ - Mac 无法编译,Windows 和 Linux 可以 - Boost ASIO?

c++ - boost::unique_lock 和 boost::shared_lock 用于读写锁

c++ - 使用 2 种方法提取容器中的数据( pop_front 和 front )

c++ - 从 C++ 调用时指定 Python 模块的目录

python - 如何在 Eclipse 或命令行中为 Google App Engine 运行 Python 代码覆盖率测试

c++ - 为什么同时包含 <iostream> 和 <fstream>

ruby-on-rails - 清除 Rails 中单元测试和功能测试之间的测试数据库 (factory_girl)

java - 如何测试一个类及其子类