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

后端 未结 3 874
时光取名叫无心
时光取名叫无心 2020-11-30 02:14

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

3条回答
  •  再見小時候
    2020-11-30 02:37

    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.

提交回复
热议问题