Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent import of function from static library

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.

like image 747
tpg2114 Avatar asked Jan 26 '12 19:01

tpg2114


1 Answers

There are a couple of methods that I know of:

  1. 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.

  2. 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.

like image 78
Matthew Slattery Avatar answered Oct 09 '22 03:10

Matthew Slattery