【问题标题】:Catch2 test don't work when building application with CMake使用 CMake 构建应用程序时,Catch2 测试不起作用
【发布时间】: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 请参阅我提供的示例。

标签: c++ cmake catch2


【解决方案1】:

快速查看Catch2 tutorial,您至少错过了要链接的 Catch2 库的规范。 该教程指出,您至少需要拥有您上面的文件似乎遗漏的代码:

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

您的示例中缺少target_link_libraries...。这可能就是您收到这些链接器错误的原因。 find_package 将引入名为 Catch2::Catch2 的库,然后您可以将其链接到您的测试可执行文件。 如果find_package command 在您的设置中不起作用,您可能需要检查您是如何安装 Catch2 的。

【讨论】:

  • 是的,这与我在评论中提供的示例相同(在显示 CMakeLists.txt 之前)。
  • 它仍然对我不起作用。当我尝试在命令行上使用 CMake 构建它时,它显示通过在 CMAKE_MODULE_PATH 中未提供“FindCatch2.cmake”,该项目已要求 CMake 查找“Catch2”提供的包配置文件,但 CMake 没有找到。找不到由“Catch2”提供的具有以下任何名称的包配置文件:Catch2Config.cmake catch2-config.cmake”我不知道从哪里获得这些文件。我认为来自 Catch2 的头文件就足够了使测试工作......
  • 要让find_package 工作,您首先需要install Catch2。如果您不想这样做,可以使用FetchContent,如here 所述。
  • 我尝试安装 Catch2 但它不起作用。我克隆了 repo 并输入了命令,但它说“C++ 编译器 MinGW/bin/g++.exe 无法编译简单的测试程序。
  • 请参阅this 问题以了解 g++ 问题。
【解决方案2】:

我假设您想将 Catch2 用作仅标头库。问题可能是因为您包含include/catch.hpp 而不是single_include/catch2/catch.hpp

【讨论】:

  • 我包含了标题“single_include/catch2/catch.hpp”,但它仍然不起作用
  • 您如何使用 Catch2?从问题上看不清楚。您是否克隆了存储库并将包含路径设置为single_include/catch2
  • 我刚刚下载了头文件并将其包含在我的 main.cpp 中
  • 您在这种情况下遇到的错误与您最初问题中的错误相同?
  • 那么也许在 Catch 存储库上打开一个问题是更好的选择。
猜你喜欢
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 2011-06-12
  • 2012-10-21
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
相关资源
最近更新 更多