经过大量艰苦的工作和研究,我成功地制作了多个 cmake 目标,以将运行我的程序与运行代码测试分开。但我不喜欢我所做的,因为我在 CMakeList.txt
文件中看到了冗余。
目前,我必须将每个新的源文件添加到两个目标,以便源目标可以使用该文件构建,而测试目标可以构建,因为它们需要测试该文件。我不能将整个源目标放入测试目标,因为那样测试目标将包含两个主要文件。
关于如何修复冗余,我唯一的想法是将源目标中没有 main.cpp
文件的所有文件放入某个组,然后将该组附加到两个目标。这样源目标只包含 main.cpp
文件和源文件组,而测试目标包含它的所有测试和源文件组。所以文件组基本上是两个目标之间的所有重叠部分。我只是不知道该怎么做。
研究
这是我发现的其他堆栈溢出问题,它们帮助我找到了现在的位置:
- what does target option mean in cmake
- how to use cmake with catch2
- cmake one build directory for multiple projects
代码
这是我用 catch2 和 cmake 进行试验的测试项目,目前适用于构建目标“tests”和“catch2Test”:
/catch2Test // <-- project folder
|---- /include
| |---- /catch2
| |---- catch.hpp
|---- /src
| |---- /myMath
| | |---- factorial.cpp
| | |---- factorial.h
| |---- main.cpp
| |---- CMakeLists.txt
|---- /test
| |---- test_main.cpp
| |---- test_factorial.cpp
| |---- CMakeLists.txt
|---- CMakeLists.txt
/include/catch2/catch.hpp
是catch2的库头文件
/src/myMath/
包含阶乘实现的头文件和代码文件,与 catch2 tutorial 中使用的相同.这也是析因测试实现的来源。
/src/main.cpp
是一个简单的主文件,其中包含 factorial.h 以进行阶乘数学运算,然后将其打印到 cout。
下面是实际重要的其余文件的显式代码:
/test/test_main.cpp
#define CATCH_CONFIG_MAIN
#include "../include/catch2/catch.hpp"
/test/test_factorial.cpp
#include "../include/catch2/catch.hpp"
#include "../src/myMath/factorial.h"
TEST_CASE("Factorials are computed", "[factorial]")
{
REQUIRE(factorial(0) == 1);
REQUIRE(factorial(1) == 1);
REQUIRE(factorial(2) == 2);
REQUIRE(factorial(3) == 6);
REQUIRE(factorial(10) == 3628800);
}
/CmakeLists.txt
cmake_minimum_required(VERSION 3.13)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(src)
add_subdirectory(test)
/test/CMakeLists.txt
add_executable(tests
../src/myMath/factorial.cpp ../src/myMath/factorial.h
../include/catch2/catch.hpp
test_main.cpp
test_factorial.cpp
)
/src/CMakeLists.txt
add_executable(catch2Test
main.cpp
myMath/factorial.cpp myMath/factorial.h
)
运行
当我运行 catch2Test 目标时,我得到了想要的结果:
Hello, World!
Factorial 0! = 1
Factorial 1! = 1
Factorial 2! = 2
Factorial 3! = 6
Factorial 6! = 720
Factorial 10! = 3628800
Process finished with exit code 0
当我运行测试目标时,我也得到了想要的结果:
===============================================================================
All tests passed (5 assertions in 1 test case)
Process finished with exit code 0
一切正常,我只是不喜欢我当前的解决方案。
如何使我的目标更容易扩展?
此外,附带问题:包含头文件库的更合适的方法是什么?我假设它不应该像我在/test/CMakeLists.txt
中那样在add_executable()
中作为../包含/catch2/catch.hpp
...
最佳答案
我想我在边栏的相关问题列表中找到了问题的答案:cmake project structure with unit tests
使用 add_library
我可以使用新的目标名称创建一个文件列表,该名称不一定包含主文件。以下是我更改子目录 CMakeList.txt
文件的方式:
src/CMakeList.txt
add_library(src
myMath/factorial.cpp myMath/factorial.h
)
add_executable(catch2Test main.cpp)
target_link_libraries(catch2Test src)
test/CMakeList.txt
add_executable(tests
../include/catch2/catch.hpp
test_main.cpp
test_factorial.cpp
)
target_link_libraries(tests src)
现在,我添加的所有源文件都进入了 src
库,我的两个目标都使用 target_link_libraries([target] src)
链接到它。
现在我想弄清楚是否可以使用 target_link_libraries 将 catch2 库附加到测试目标。
编辑: 我想我在这里找到了第二个问题的答案:' cmake os x failure ar no archive members specific ' 和这里:' how to create a cmake header only library that depends on external header files '.
我试图用单个头文件创建一个库,但 cmake 显然无法识别头文件。现在,我的 /include
文件夹中有一个 CMakeLists.txt
,如下所示:
add_library(catch2 INTERFACE)
target_sources(catch2 INTERFACE catch2/catch.hpp)
我已将 /include
文件夹添加到我的主 CMakeLists.txt
文件中的添加子目录列表中。
此外,我的测试目标现在使用 target_link_libraries(tests catch2)
链接 catch2 库,我发现这比在测试目标的可执行文件列表中放置一个紧张的目录链接要好得多。
关于c++ - 如何使我的 cmake 目标用于 catch2 测试和项目运行更具可扩展性和合理性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55963115/