首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >catch2单-包括测试中的“未定义引用”原因

catch2单-包括测试中的“未定义引用”原因
EN

Stack Overflow用户
提问于 2019-05-18 00:10:25
回答 1查看 2.5K关注 0票数 0

我见过许多关于同样问题的例子,但它们不适用于我的情况。

我有以下文件结构:

代码语言:javascript
复制
unit-tests/CMakeLists.txt
unit-tests/FlaAlgoTests/CMakeLists.txt
unit-tests/FlaAlgoTests/catch_test_runner.cpp

Proj/CMakeLists.txt

代码语言:javascript
复制
ExternalProject_Add(
        catch
        PREFIX ${CMAKE_BINARY_DIR}/catch2
        GIT_REPOSITORY https://github.com/catchorg/Catch2.git
        TIMEOUT 10
        UPDATE_COMMAND ${GIT_EXECUTABLE} pull
        CONFIGURE_COMMAND ""
        BUILD_COMMAND ""
        INSTALL_COMMAND ""
        LOG_DOWNLOAD ON
)
ExternalProject_Get_Property(catch source_dir)
include_directories(${source_dir}/single_include/catch2)

Proj/TestDir1/CMakeLists.txt

代码语言:javascript
复制
add_executable(
        catch_test_runner
        catch_test_runner.cpp
)

其中catch_test_runner.cpp是:

代码语言:javascript
复制
#include <iostream>
#include "catch.hpp"

#define CATCH_CONFIG_MAIN

TEST_CASE("Yeet", "[beep]"){
    REQUIRE(true);
    REQUIRE(2 == 1);
}

所有这些看起来都很好,似乎基本上是我找到的众多指南的拷贝/粘贴。然而,我得到以下问题:

代码语言:javascript
复制
[100%] Linking CXX executable catch_test_runner.exe
CMakeFiles/catch_test_runner.dir/catch_test_runner.o: In function `::____C_A_T_C_H____T_E_S_T__(void)':
/cygdrive/c/Users/dbak/Projects/firmware/unit-tests/FlaAlgoTests/catch_test_runner.cpp:7: undefined reference to `Catch::StringRef::StringRef(char const*)'
/cygdrive/c/Users/dbak/Projects/firmware/unit-tests/FlaAlgoTests/catch_test_runner.cpp:7:(.text+0x32): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Catch::StringRef::StringRef(char const*)'
/cygdrive/c/Users/dbak/Projects/firmware/unit-tests/FlaAlgoTests/catch_test_runner.cpp:7: undefined reference to `Catch::AssertionHandler::AssertionHandler(Catch::StringRef const&, Catch::SourceLineInfo const&, Catch::StringRef, Catch::ResultDisposition::Flags)'

所以,很明显,它能够链接到catch.hpp,否则它就找不到这些东西了。显然,我使用的是single_include版本的catch,这是我在同一问题上找到的所有示例的问题。

我做错什么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-18 08:09:24

#define CATCH_CONFIG_MAIN告诉catch.hpp报头,它也应该发出Catch实现。但为此目的,这一行应该出现在之前,,包括catch.hpp

代码语言:javascript
复制
#define CATCH_CONFIG_MAIN // This should come **before** including the 'catch.hpp'.
#include "catch.hpp"
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56194930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档