Catch2 链接器错误

问题描述

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

testmain.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 文件来提供其符号?

解决方法

感谢response to one of the Catch2 Github Issues

horenmar:

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 中。