If I have the same symbol defined in an object file and in a library the GNU linker takes the symbol from the object file. Consider this example:
g++ -L"dir/to/lib" -o Executable Test.o foo.o -lMyLib
If I have defined a function foo
with the same signature in both foo.cpp and in a source file "MyLib" was compiled from, the GNU linker always prefers the one from the former if I use this order.
Is this behaviour GNU toolchain specific? Do you know from other linkers that behave the same way? Is this anywhere documented (GNU documentation, C++ standard)? I couldn't find anything...
I would like to use this feature to replace/mock certain functions while doing unit testing (aka link seam).
From http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html:
"the linker searches and processes libraries and object files in the order they are specified"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With