【发布时间】:2020-02-17 09:53:41
【问题描述】:
我正在阅读 James W. Grenning 的“嵌入式 C 的测试驱动开发”。
我想使用 Visual Studio Community 2019 和 gTest 重现带有“链接时间替换”的案例。
我有以下代码:
production_code 静态链接库
foo.cpp
#include "foo.h"
int foo(int x) {
return x + 1;
}
foo.h
#ifndef _foo_
#define _foo_
int foo(int x);
#endif //_foo_
在 gtest 项目中,production_code 库通过引用包含在内 test.cpp
#include "gtest\gtest.h"
#include "gmock\gmock.h"
#include "..\prod\foo.h"
//fake implementation of production code foo
int foo(int x) {
return x - 1;
}
TEST(TestCaseName, TestName) {
auto x = foo(5);
EXPECT_EQ(x, 4);
}
链接器给了我以下错误:
1>prod.lib(foo.obj):错误 LNK2005:“int __cdecl foo(int)” (?foo@@YAHH@Z) 已在 test.obj 1>C:\Example\prod_test.exe 中定义 : 致命错误 LNK1169: 找到一个或多个多重定义的符号
我在这里错过了什么?为什么这不起作用?
如果我将命令“/FORCE:MULTIPLE”添加到链接器,那么我只会收到警告,但我认为这不是正确的做法。
【问题讨论】:
-
foo的两种不同定义打破了单一定义规则(ODR):en.cppreference.com/w/cpp/language/definition 因此作者提出的技术不受标准支持。 "...每个非内联函数或 odr 使用的变量(见下文)的一个且只有一个定义需要出现在整个程序(包括任何标准和用户定义的库)中。.. 。” -
一个函数不能有两个不同的定义。如果你想使用假的,你必须在构建 UT 时从构建系统中排除真正的实现。如果您想正确执行此操作,请使用 GMock 模拟和依赖注入。而 FORCE:MULTIPLE 很可能只是让链接器“随机选择实现”,这不是你想要的。
标签: c++ linker-errors googletest