Say I have two static libraries that were not built by me and I have no control over their contents.
Library 1 has functions:
A()
B()
C()
Library 2 has functions:
A()
D()
E()
Both need to be linked into a calling application but the naming conflict of A()
throws errors.
Is there a way to say "Ignore A() from Library 1 when linking" in linux using gcc and ld.
There are a couple of methods that I know of:
You could make a copy of the library which has the relevant symbol hidden, and link against the copy. You don't need access to any of the source for the library code to do this: objcopy
can do it with the --localize-symbol
option. I describe how to do this with .o
files in this answer to a similar question, but the same method works just as well with .a
libraries.
The --allow-multiple-definition
option could be used. (If you're linking via a gcc
command, rather than with ld
directly, you'll need to specify the option as -Wl,--allow-multiple-definition
.) This will cause the linker to stop caring about the multiple definition, and simply use the first one that it encounters instead - so you have to be careful what order the libraries appear in on the command line. The downside it that it's a global option, so if you have other unexpected symbol clashes, it might quitely do the wrong thing instead of telling you about it.
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