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

后端 未结 3 878
时光取名叫无心
时光取名叫无心 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:19

    This problem isn't necessarily Xcode or Objective-C related. Don't link/archive libraries into other libraries. A & B only depend on C at final link time, not when they're built. You want:

    1. build A
    2. build B
    3. build C
    4. build app & link

    Here's an example project I made to demonstrate:

    Makefile:

    app: main.o a.a b.a c.a
            gcc $^ -o $@
    
    %.o: %.c
            gcc -Wall -c $^
    
    %.a: %.o
            ar -r $@ $^
    
    clean:
            rm -rf *.o *.a app
    

    a.c:

    #include 
    void c(void);
    
    void a(void)
    {
      printf("a\n");
      c();
    }
    

    b.c:

    #include 
    void c(void);
    
    void b(void)
    {
      printf("b\n");
      c();
    }
    

    c.c:

    #include 
    
    void c(void)
    {
      printf("c\n");
    }
    

    main.c:

    #include 
    
    void a(void);
    void b(void);
    
    int main(int argc, char *argv[])
    {
      a();
      b();
      return 0;
    }
    

    Build and run log:

    $ make
    gcc -Wall -c main.c
    gcc -Wall -c a.c
    ar -r a.a a.o
    ar: creating archive a.a
    gcc -Wall -c b.c
    ar -r b.a b.o
    ar: creating archive b.a
    gcc -Wall -c c.c
    ar -r c.a c.o
    ar: creating archive c.a
    gcc main.o a.a b.a c.a -o app
    rm a.o b.o c.o
    $ ./app 
    a
    c
    b
    c
    

提交回复
热议问题