我有以下项目结构:
test_main.cc
#define CATCH_CONFIG_MAIN
#include "catch2.hpp"
测试1.cc
#include "catch2.hpp"
#include "test_utils.hpp"
TEST_CASE("test1", "[test1]") {
REQUIRE(1 == 1);
}
测试2.cc
#include "catch2.hpp"
#include "test_utils.hpp"
TEST_CASE("test2", "[test2]") {
REQUIRE(2 == 2);
}
test_utils.hpp
#pragma once
#include <iostream>
void something_great() {
std::cout << ":)\n";
}
如果我使用类似的东西进行编译clang++ -std=c++17 test_main.cc test1.cc test2.cc
,则该函数something_great
在 test1.o 和 test2.o 中都定义了。这会导致错误,例如
duplicate symbol __Z15something_greatv in:
test1.cc.o
test2.cc.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
在 Catch2 文档的Scaling Up部分中,他们提到为了拆分您的测试,您可能需要
根据测试需要使用尽可能多的附加 cpp 文件(或任何你称之为实现文件的文件),但分区对你的工作方式最有意义。每个附加文件只需要 #include "catch.hpp"
但是在文档的示例部分中,我没有看到像我这样的用例。我阅读了这篇博文,其中描述了三种对我不感兴趣的解决方案:将函数定义为宏,或将函数static
或inline
.
是否有另一种方法来编译这些文件,这些文件产生一个可执行文件,其主要功能由定义test_main.cc
?