我正在尝试使用 Catch2 测试来测试我的程序并使用 CMake 构建应用程序。当未实施 Catch2 测试时,该程序可以运行并且应用程序将被构建。一旦我实现了 Catch2 测试,应用程序将不再构建。我包含#define CONFIG_CATCH_MAIN并#include "catch.hpp"进入 main.cpp。
但是当我尝试构建包含测试的应用程序时,我总是会得到这样的结果:
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)'
我不知道发生了什么...
我的 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") ;
}
我的 CMakeLists.txt 看起来像这样:
cmake_minimum_required(VERSION 3.20.2)
project(intent_recognition)
add_executable(intent_recognition main.cpp)