如何将源文件包含在其他目录树的Bool.Test案例中?

时间:2012-02-18 21:56:48

标签: 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/model添加到include_directories中的tests/CMakeLists.txt命令来解决它。有了这个,应该使用#include "model_a.cpp"

1 个答案:

答案 0 :(得分:1)

您似乎错过了PARENT_SCOPE,以便将${model_SOURCES}的值从src/model/CMakeLists.txt提升到tests/model/CMakeLists.txt

尝试使用

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