如何使用于 catch2 测试和项目 运行 的 cmake 目标更具可扩展性和合理性?

How do I make my cmake targets for catch2 testing and project running more extendable and sensible?

经过大量的努力和研究,我已经成功地制作了多个 cmake 目标,以将 运行ning 我的程序与 运行ning 代码测试分开。但我不喜欢我所做的,因为我在 CMakeList.txt 文件中看到了冗余。

目前我必须将每个新的源文件添加到两个目标,以便源目标可以使用该文件构建,测试目标可以构建,因为它们需要测试该文件。我不能将整个源目标放入测试目标中,因为那样测试目标将包含两个主要文件。

我对如何修复冗余的唯一想法是将源目标中没有 main.cpp 文件的所有文件放入某个组,然后将该组附加到两个目标。这样源目标只包含 main.cpp 文件和源文件组,而测试目标包含它的所有测试和源文件组。所以文件组基本上是两个目标之间的所有重叠部分。我只是不知道该怎么做。

研究

这是我发现的其他堆栈溢出问题,它们帮助我找到了现在的位置:

代码

这是我用 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

一切正常,我只是不喜欢我目前的解决方案。

如何使我的目标更容易扩展?

另外,附带问题:包含头文件库的更合适的方法是什么?我假设它 而不是 应该在 add_executable() 中,就像我在 /test/CMakeLists.txt 中所做的那样 ../include/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 库,我的两个目标 link 使用 target_link_libraries([target] src).

现在我想弄清楚是否可以使用 target_link_libraries 将 catch2 库附加到测试目标。

编辑: 我想我在这里找到了第二个问题的答案:'cmake os x failure ar no archive members specific' and here: ''。

我试图用单个头文件创建一个库,但 cmake 显然无法识别头文件。现在我的 /include 文件夹中有一个 CMakeLists.txt,看起来像:

add_library(catch2 INTERFACE)
target_sources(catch2 INTERFACE catch2/catch.hpp)

我已将 /include 文件夹添加到我的主 CMakeLists.txt 文件中的已添加子目录列表中。

我的测试现在还通过使用 target_link_libraries(tests catch2) links catch2 库,我发现这比将紧张的目录 link 放在测试的可执行文件列表中要好得多目标。