【发布时间】:2021-05-03 07:29:27
【问题描述】:
我正在尝试使用 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)
【问题讨论】:
-
你如何在
CMakeLists.txt中使用cach2?它works for me. -
好吧,我不知道测试框架,但那些错误似乎是链接器错误。您是否有实现这些功能的库正确链接到测试可执行文件?请将您的 CMakeLists.txt 放入您的问题中!
-
所以问题无效
CMakeLists.txt请参阅我提供的示例。