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

C++ 如何使catch2测试和项目运行的cmake目标更具可扩展性和合理性?,c++,unit-testing,cmake,catch-unit-test,C++,Unit Testing,Cmake,Catch Unit Test,经过大量的努力和研究,我已经成功地创建了多个cmake目标,将运行我的程序与运行代码测试分离开来。但是我不喜欢我所做的,因为我在CMakeList.txt文件中看到冗余 目前,我必须将每个新的源文件添加到两个目标中,以便源目标可以使用该文件进行构建,而测试目标可以进行构建,因为它们需要测试该文件。我不能将整个源目标抛出到测试目标中,因为这样测试目标将包含两个主文件 我唯一知道如何修复冗余的方法是将源目标中没有main.cpp文件的所有文件放入某个组,然后将该组附加到两个目标。这样,源目标只包含m

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

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

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

研究 以下是我发现的其他堆栈溢出问题,这些问题帮助我找到了自己的位置:

代码 下面是我的测试项目,我用catch2和cmake进行了实验,目前这两个测试都适用于构建目标“测试”和“catch2Test”:

/test/test\u factorial.cpp

#define CATCH_CONFIG_MAIN
#include "../include/catch2/catch.hpp"
#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)
add_executable(tests
        ../src/myMath/factorial.cpp ../src/myMath/factorial.h

        ../include/catch2/catch.hpp

        test_main.cpp
        test_factorial.cpp
)
add_executable(catch2Test
        main.cpp
        myMath/factorial.cpp myMath/factorial.h
)
add_library(src
        myMath/factorial.cpp myMath/factorial.h
)

add_executable(catch2Test main.cpp)

target_link_libraries(catch2Test src)
add_executable(tests
        ../include/catch2/catch.hpp

        test_main.cpp
        test_factorial.cpp
)

target_link_libraries(tests src)
/test/CMakeLists.txt

cmake_minimum_required(VERSION 3.13)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(src)
add_subdirectory(test)
add_executable(tests
        ../src/myMath/factorial.cpp ../src/myMath/factorial.h

        ../include/catch2/catch.hpp

        test_main.cpp
        test_factorial.cpp
)
add_executable(catch2Test
        main.cpp
        myMath/factorial.cpp myMath/factorial.h
)
add_library(src
        myMath/factorial.cpp myMath/factorial.h
)

add_executable(catch2Test main.cpp)

target_link_libraries(catch2Test src)
add_executable(tests
        ../include/catch2/catch.hpp

        test_main.cpp
        test_factorial.cpp
)

target_link_libraries(tests src)
/src/CMakeLists.txt

cmake_minimum_required(VERSION 3.13)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(src)
add_subdirectory(test)
add_executable(tests
        ../src/myMath/factorial.cpp ../src/myMath/factorial.h

        ../include/catch2/catch.hpp

        test_main.cpp
        test_factorial.cpp
)
add_executable(catch2Test
        main.cpp
        myMath/factorial.cpp myMath/factorial.h
)
add_library(src
        myMath/factorial.cpp myMath/factorial.h
)

add_executable(catch2Test main.cpp)

target_link_libraries(catch2Test src)
add_executable(tests
        ../include/catch2/catch.hpp

        test_main.cpp
        test_factorial.cpp
)

target_link_libraries(tests src)
跑步 当我运行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
当我运行目标测试时,我也会得到期望的结果:

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()
中,
/include/catch2/catch.hpp

我想我在侧栏的相关问题列表中找到了我问题的答案:

使用
add_library
我可以创建一个具有新目标名称的文件列表,该名称不一定包含主文件。下面是我如何更改子目录
CMakeList.txt
文件的:

src/CMakeList.txt

cmake_minimum_required(VERSION 3.13)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(src)
add_subdirectory(test)
add_executable(tests
        ../src/myMath/factorial.cpp ../src/myMath/factorial.h

        ../include/catch2/catch.hpp

        test_main.cpp
        test_factorial.cpp
)
add_executable(catch2Test
        main.cpp
        myMath/factorial.cpp myMath/factorial.h
)
add_library(src
        myMath/factorial.cpp myMath/factorial.h
)

add_executable(catch2Test main.cpp)

target_link_libraries(catch2Test src)
add_executable(tests
        ../include/catch2/catch.hpp

        test_main.cpp
        test_factorial.cpp
)

target_link_libraries(tests src)
test/CMakeList.txt

cmake_minimum_required(VERSION 3.13)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(src)
add_subdirectory(test)
add_executable(tests
        ../src/myMath/factorial.cpp ../src/myMath/factorial.h

        ../include/catch2/catch.hpp

        test_main.cpp
        test_factorial.cpp
)
add_executable(catch2Test
        main.cpp
        myMath/factorial.cpp myMath/factorial.h
)
add_library(src
        myMath/factorial.cpp myMath/factorial.h
)

add_executable(catch2Test main.cpp)

target_link_libraries(catch2Test src)
add_executable(tests
        ../include/catch2/catch.hpp

        test_main.cpp
        test_factorial.cpp
)

target_link_libraries(tests src)
现在,我添加的所有源文件都进入
src
库,我的两个目标都使用
target\u link\u库([target]src)
链接到它

现在,我试图弄清楚是否可以使用target_link_库将catch2库附加到测试目标

编辑:我想我在这里找到了第二个问题的答案:''和:''

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

add_library(catch2 INTERFACE)
target_sources(catch2 INTERFACE catch2/catch.hpp)
我已将
/include
文件夹添加到我的主
CMakeLists.txt
文件中添加的子目录列表中

另外,我的测试目标现在通过使用
target\u link\u libraries(tests-catch2)
链接catch2库,我发现这比在测试目标的可执行文件列表中放置一个紧张的目录链接要好得多