Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid "duplicate symbol" errors in xcode with shared static libraries?

I have static libraries A, B and C organized into Xcode projects. A and B depend on C. When I build an iPhone project that depends on A and B, I get a linker error that a duplicate symbol (from C) was detected in A and B. How can I organize these three static libraries so I can include them in other Xcode projects without experiencing this error?

like image 222
richcollins Avatar asked Feb 20 '10 03:02

richcollins


1 Answers

Carl's answer is right, but for the wrong reasons: there's actually nothing wrong with linking static libraries together, as we can see using Carl's own sample. Set-up Carl's sample code and then do this: (I use libtool because that is what XCode uses)

neutron:libtest jamie$ libtool -o a2.a a.a c.a neutron:libtest jamie$ libtool -o b2.a b.a c.a neutron:libtest jamie$ gcc main.o a2.a b2.a -o app2 neutron:libtest jamie$ ./app2 a c b c neutron:libtest jamie$  

This links a2.a and b2.a with main.o. According to Carl, this is the source of OPs problem, and app2 shouldn't link. But of course it does. The linker is smart enough to ignore two instances of the same file. We can see that both a2.a and b2.a contain c.o:

neutron:libtest jamie$ ar -t a2.a __.SYMDEF SORTED a.o c.o neutron:libtest jamie$ ar -t b2.a __.SYMDEF SORTED b.o c.o 

Yet it links fine.

The problem is, I believe, linked to Universal Binaries, either PPC/x86 universal binaries, or armv6/armv7 iPhone universal binaries. The problem here is that there is a bug with categories and the fix (add -all_load to the linker flags) is a fix that only works for single architectures. Using -all_load breaks the linkers ability to ignore symbols that are defined for multiple architectures, and you have your duplicate symbol error.

I wrote about it here including a better solution than using -all_load.

like image 197
jamie Avatar answered Sep 21 '22 21:09

jamie