【问题标题】:How to include source file into Bool.Test case from other directory tree?如何将源文件从其他目录树包含到 Bool.Test 用例中?
【发布时间】:2012-02-18 21:56:48
【问题描述】:

这是我上一个问题的后续问题: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)

我如何必须在我的tests/model/model_a_test.cpp 中包含src/model/model_a.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添加到tests/CMakeLists.txt中的include_directories命令解决了。有了这个,应该使用#include "model_a.cpp"

【问题讨论】:

    标签: c++ unit-testing boost cmake


    【解决方案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)
    

    【讨论】:

    • 感谢您的回答。然而,这并没有帮助。但是通过摆弄您的解决方案,我尝试了我在问题编辑中描述的内容。由于您的回答使我重新考虑了包含来源的方式,因此我会接受您的回答。
    猜你喜欢
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    相关资源
    最近更新 更多