首页 > 解决方案 > Catch2 链接器错误

问题描述

我已将Catch2作为子模块添加到我的项目中,并Catch2/include/catch.hpp使用以下代码包含标题:

测试main.cpp:

#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"

TEST_CASE( "Test test", "[test]" ) {
    REQUIRE(true);
}

但我得到一个链接器错误:

Undefined symbols for architecture x86_64: "Catch::NameAndTags::NameAndTags(Catch::StringRef const&, Catch::StringRef const&)", referenced from: ___cxx_global_var_init.1 in testmain.cpp.o

我究竟做错了什么?我认为 Catch2 应该在其标题中是自包含的,并且不需要任何.cpp文件来提供其符号?

标签: c++testingclangcatch2

解决方案


感谢对Catch2 Github 问题之一的回应

霍伦马尔:

You are supposed to use the single-include version.

It is possible to use the headers in include/, but then you have to also compile and
link the implementation files in there and you are not provided with any guarantees
vis-a-vis stability and functionality.

中的版本includes/是生成“真实”Catch2 标头的原始文件。你应该包括的那个是在Catch2/single_include/catch2/catch.hpp. 因此,请确保您的标题搜索路径设置为在那里搜索catch.hpp,而不是在includes.


推荐阅读