所以我在 2 个独立的翻译单元中有这段代码:
// a.cpp
#include <stdio.h>
inline int func() { return 5; }
int proxy();
int main() { printf("%d", func() + proxy()); }
// b.cpp
inline int func() { return 6; }
int proxy() { return func(); }
正常编译时,结果为10
. 当使用 -O3(内联)编译时,我得到11
.
我显然违反了func()
.
当我开始将不同 dll 的源合并到更少的 dll 中时,它就出现了。
我努力了:
- GCC 5.1
-Wodr
(需要-flto
) - 黄金链接器
-detect-odr-violations
ASAN_OPTIONS=detect_odr_violation=1
在使用地址清理程序运行检测的二进制文件之前进行设置。
据推测,Asan 可以捕获其他 ODR 违规行为(具有不同类型的全局变量或类似的东西......)
这是一个非常讨厌的 C++ 问题,我很惊讶没有可靠的工具来检测它。
也许我误用了我尝试过的工具之一?或者有什么不同的工具可以做到这一点?
编辑:
即使我做了两个func()
完全不同的实现,这个问题仍然没有被注意到,所以它们不会被编译成相同数量的指令。
这也会影响在类体内定义的类方法——它们是隐式内联的。
// a.cpp
struct A { int data; A() : data(5){} };
// b.cpp
struct A { int data; A() : data(6){} };
之后有大量复制/粘贴 + 小修改的遗留代码是一种乐趣。