简体   繁体   English

使用 CMake 构建应用程序时,Catch2 测试不起作用

[英]Catch2 test don't work when building application with CMake

I'm trying to test my program by using Catch2 tests and build the application using CMake.我正在尝试使用 Catch2 测试来测试我的程序,并使用 CMake 构建应用程序。 The program works and the application gets build when the Catch2 tests are not implemented.当未实施 Catch2 测试时,该程序可以运行并且应用程序将被构建。 Once I implemented the Catch2 tests, the application won't build anymore.一旦我实现了 Catch2 测试,应用程序将不再构建。 I included #define CONFIG_CATCH_MAIN and #include "catch.hpp" into main.cpp.我将#define CONFIG_CATCH_MAIN#include "catch.hpp"包含在main.cpp 中。

But I always get something like this when I try to build the application with the tests included:但是当我尝试构建包含测试的应用程序时,我总是会得到这样的结果:

CMakeFiles\intent_recognition.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x43d): undefined reference to Catch::StringRef::StringRef(char const*)' CMakeFiles\intent_recognition.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x4b9): undefined reference to Catch::AssertionHandler::AssertionHandler(Catch::StringRef const&, Catch::SourceLineInfo const&, Catch::StringRef, Catch::ResultDisposition::Flags)' CMakeFiles\intent_recognition.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x43d): 未定义引用Catch::StringRef::StringRef(char const*)' CMakeFiles\intent_recognition.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x4b9): undefined reference to Catch::AssertionHandler::AssertionHandler(Catch::StringRef const&, Catch::SourceLineInfo const&, Catch::StringRef, Catch::ResultDisposition::Flags)'

I don't know what's going...我不知道发生了什么...

My main.cpp file looks like this:我的 main.cpp 文件如下所示:

#define CONFIG_CATCH_MAIN
#include "catch.hpp"

#include <iostream>
#include <string>
#include "Intent.h"

std::string func (std::string input)
{
    std::cout << input << std::endl;

    Intent IntentObj = Intent(input);       // create Intent Object with input

    IntentObj.analyze();

    return IntentObj.result();
}

TEST_CASE("Get Weather", "[func]")
{
    REQUIRE( func("What is the weather like today?") == "Intent: Get Weather" );
    REQUIRE( func("Tell me what the weather is like.") == "Intent: Get Weather") ;
    REQUIRE( func("I want to know the weather for tomorrow") == "Intent: Get Weather" );
    REQUIRE( func("Can you tell me the weather for Wednesday?") == "Intent: Get Weather") ;
} 

My CMakeLists.txt looks like this:我的 CMakeLists.txt 看起来像这样:

cmake_minimum_required(VERSION 3.20.2)

project(intent_recognition)

add_executable(intent_recognition main.cpp)

Having a quick look at the Catch2 tutorial you are at least missing the specification of the Catch2 library to link.快速浏览一下Catch2 教程,您至少错过了要链接的 Catch2 库的规范。 The tutorial states that you are at least need to have this code which your file above seems to miss:该教程指出,您至少需要拥有上面的文件似乎遗漏的代码:

find_package(Catch2 REQUIRED)
add_executable(tests test.cpp)
target_link_libraries(tests PRIVATE Catch2::Catch2)

The target_link_libraries... is missing in your example.您的示例中缺少target_link_libraries... That's probably why you are getting those linker errors.这可能就是您收到 linker 错误的原因。 The find_package will introduce the library under the name Catch2::Catch2 which you can then link to your test executable. find_package将引入名为Catch2::Catch2的库,然后您可以将其链接到您的测试可执行文件。 If the find_package command is not working in your setup, you may need to check how you did install Catch2.如果find_package command在您的设置中不起作用,您可能需要检查您是如何安装 Catch2 的。

I'm assuming that you want to use Catch2 as a header only library.我假设您想将 Catch2 用作 header 唯一库。 The problem may be because you are including include/catch.hpp instead of single_include/catch2/catch.hpp .问题可能是因为您包含include/catch.hpp而不是single_include/catch2/catch.hpp

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

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