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:
objcopycan do it with the--localize-symboloption. I describe how to do this with.ofiles in this answer to a similar question, but the same method works just as well with.alibraries.The
--allow-multiple-definitionoption could be used. (If you’re linking via agcccommand, rather than withlddirectly, 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.