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

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.

Leave a Comment