简体   繁体   English

如何使我的 cmake 目标用于 catch2 测试和项目运行更可扩展和更明智?

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

After a lot of hard work and research, I've managed to make multiple cmake targets to separate running my program from running tests on the code.经过大量的辛勤工作和研究,我设法制作了多个 cmake 目标,以将运行我的程序与运行代码测试分开。 But I don't like what I've made because I see redundancy in the CMakeList.txt files.但我不喜欢我所做的,因为我在CMakeList.txt文件中看到了冗余。

Currently I have to add every new source file to both targets so that the source target can build using that file and the test target can build since they need to test that file.目前,我必须将每个新源文件添加到两个目标中,以便源目标可以使用该文件进行构建,而测试目标可以构建,因为它们需要测试该文件。 I can't throw the entire source target into the test target because then the test target would contain two main files.我不能将整个源目标扔到测试目标中,因为这样测试目标将包含两个主要文件。

My only idea of how to fix the redundancy is to put all of the files in the source target without the main.cpp file into some group, and then attach that group to both targets.我关于如何修复冗余的唯一想法是将源目标中没有main.cpp文件的所有文件放入某个组,然后将该组附加到两个目标。 That way the source target contains just the main.cpp file and the source file group, and the test target contains all its tests and the source file group.这样源目标只包含main.cpp文件和源文件组,而测试目标包含它的所有测试和源文件组。 So the file group would basically be all of the overlap between the two targets.所以文件组基本上是两个目标之间的所有重叠。 I just don't know how to do that.我只是不知道该怎么做。

Research研究

Here's the other stack overflow questions I found that helped me get to where I am:这是我发现的其他堆栈溢出问题,它们帮助我到达了我的位置:

Code代码

Here is my test project I made to experiment with catch2 and cmake that currently works for both build targets "tests" and "catch2Test":这是我用来试验 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 is the library header file for catch2 /include/catch2/catch.hpp/include/catch2/catch.hpp的库头文件

/src/myMath/ contains the header file and code file for the factorial implementation, same as used in the catch2 tutorial . /src/myMath/包含阶乘实现的头文件和代码文件,与catch2 教程中使用的相同。 This is also where the implementation for the factorial tests come from.这也是阶乘检验的实现的来源。

/src/main.cpp is a simple main file that includes factorial.h to do factorial math and then prints it to cout. /src/main.cpp是一个简单的主文件,其中包含 factorial.h 以进行阶乘数学运算,然后将其打印到 cout。

Here's explicit code for the remaining files that actually matter:以下是实际重要的其余文件的显式代码:

/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
)

Running跑步

When I run the catch2Test target, I get desired results:当我运行 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

And when I run the tests target, I also get desired results:当我运行测试目标时,我也得到了想要的结果:

===============================================================================
All tests passed (5 assertions in 1 test case)


Process finished with exit code 0

It all works, I just don't like my current solution.一切正常,我只是不喜欢我目前的解决方案。

How can I make my targets more easily extendable?如何让我的目标更容易扩展?

Also, side question: what's the more appropriate way to include a header library?另外,附带问题:包含头库的更合适的方法是什么? I assume it's not supposed to be in the add_executable() like I did in /test/CMakeLists.txt as ../include/catch2/catch.hpp ...我认为它应该像我在/test/CMakeLists.txt那样在add_executable()中作为../include/catch2/catch.hpp ...

I think I found an answer to my question in the list of related questions in the sidebar: cmake project structure with unit tests我想我在侧边栏的相关问题列表中找到了我的问题的答案: cmake project structure with unit tests

Using add_library I can create a list of files with a new target name that doesn't necessarily contain a main file.使用add_library我可以创建一个具有新目标名称的文件列表,该名称不一定包含主文件。 Here's how I've changed my subdirectory CMakeList.txt files:下面是我如何更改我的子目录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)

Now all my source files that I add go into the src library and both of my targets link to it using target_link_libraries([target] src) .现在我添加的所有源文件都进入src库,我的两个目标都使用target_link_libraries([target] src)链接到它。

Now I'm trying to figure out if I can use target_link_libraries to attach the catch2 lib to the tests target.现在我想弄清楚是否可以使用 target_link_libraries 将 catch2 lib 附加到测试目标。

Edit: I think I found my answer to my second question here: ' cmake os x failure ar no archive members specific ' and here: ' how to create a cmake header only library that depends on external header files '.编辑:我想我在这里找到了我的第二个问题的答案:' cmake os x failure ar no archive members specific '和这里:' how to create a cmake header only library that取决于外部头文件'。

I tried to make a library out of a single header file and cmake doesn't recognize header files apparently.我试图从单个头文件中创建一个库,而 cmake 显然无法识别头文件。 I now have a CMakeLists.txt in my /include folder that looks like:我现在在/include文件夹中有一个CMakeLists.txt ,如下所示:

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

I've added the /include folder to the list of added subdirectories in my main CMakeLists.txt file.我已将/include文件夹添加到我的主CMakeLists.txt文件中添加的子目录列表中。

Also my tests target now links the catch2 library by using target_link_libraries(tests catch2) which I find much nicer than putting a strained directory link inside the list of executable files for the tests target.此外,我的测试目标现在使用target_link_libraries(tests catch2)链接 catch2 库,我发现这比在测试目标的可执行文件列表中放置一个紧张的目录链接要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM