A Python script to check your C++ compiled stuff for One Definition Rule violations.
Because debugging ODRV is cumbersome, not always trivial and it's better to catch it at build-time rather than at run-time with sanitizers.
Sanitizers are awesome, though. Use sanitizers.
at least hand sanitizers
It should be noted, that this task could be better done within compiler or linker themselves, where code model is available for more scrutinized analysis.
Ideally, we would like to have a compiler option, something like -Wodr
and have violations reported before linking procedure. No known compiler does this.
//foo.cpp
char func(char c) { return c; }
//bar.cpp
char func(char) { return 'b'; }
add_library(foo STATIC foo.cpp)
add_library(bar STATIC bar.cpp)
target_link_libraries(app foo bar)
[6/6] Linking CXX executable app
multiple definitions of func(char):
in file foo.cpp.o
in file bar.cpp.o
By dumping symbols from compiled .o
s, archived .a
s and linked .so
and ensuring that there are no symbols having same signature and different implementation.
- currently it is a proof of concept, not guaranteed to work for all cases
- doesn't catch everything, very likely to catch more than needed
- potentially slow on large binaries
-
if linux
:pacman
,yum
,apt
or evenmake
install yourself Python 3.6+,readelf
andc++filt
(usually inbinutils
package)if windows
: getdumpbin.exe
from Visual Studio accessible inPATH
envvar (e.g. viavcvarsall.bat
or VS Command Prompt)
- get
odr.py
someplace where you can execute it - run
odr.py object.o more/object.o and/libstatic.a and/even/libdynamic.so
- meditate on the output and see if it uncovers something nasty
Best served hot and integrated with build system of your choice!
See odr.cmake
for an example of integration with CMake.