English 中文(简体)
如何使用共享静态库避免 Xcode 中的“重复符号”错误?
原标题:How can I avoid "duplicate symbol" errors in xcode with shared static libraries?

我有静态库A、B和C,这些库被组织到Xcode项目中。A和B依赖于C。当我构建一个依赖于A和B的iPhone项目时,我收到一个链接器错误,即在A和B中检测到重复符号(来自C)。我该如何组织这三个静态库,以便我可以将它们包含到其他Xcode项目中而不会遇到此错误?

问题回答

Carl的答案是正确的,但理由错误:实际上,将静态库链接在一起没有问题,这可以通过使用Carl自己的示例看出来。设置Carl的示例代码,然后执行以下操作:(我使用libtool,因为这是XCode使用的)

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$ 

这将a2.a和b2.a与main.o链接起来。据Carl说,这是OP问题的根源,app2不应该链接。但是当然它会链接。链接器足够聪明,可以忽略同一个文件的两个实例。我们可以看到a2.a和b2.a都包含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

然而它链接良好。

问题是,我相信与通用二进制有关,无论是PPC/x86通用二进制还是armv6/armv7 iPhone通用二进制。 这里的问题在于类别存在一个错误,修复方法(在链接器标志中添加-all_load)是仅适用于单个架构的修复方法。使用-all_load将破坏链接器忽略定义为多个架构的符号的能力,从而导致重复符号错误。

我在这里写了关于它的文章包括一个比使用-all_load更好的解决方案。

使用-all_load的替代方案是仅在需要的库中使用-force_load"path_to_lib"。例如,您可以使用类似于: -force_load "$(PROJECT_DIR)/libname"的东西。

这样可以避免你需要为Jamie的解决方案进行修改实现文件的工作。

这是three20项目采取的解决方案: http://groups.google.com/group/three20/browse_thread/thread/ec208be4ff8b4dcb/0dccf992a26850df

编辑:从Xcode 4.3开始,不再需要-all_load-force_load。现在只需要-ObjC。有关更多详细信息,请参见https://stackoverflow.com/a/2615407/211292

这个问题并不一定与Xcode或Objective-C相关。不要将库链接/存档到其他库中。A和B只在最终链接时依赖于C,而不是在构建它们时。您想要:

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

这是我制作的一个示例项目,用于演示:

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 <stdio.h>
void c(void);

void a(void)
{
  printf("a
");
  c();
}

公元前

#include <stdio.h>
void c(void);

void b(void)
{
  printf("b
");
  c();
}

I'm sorry, but "c.c:" does not have a clear meaning or context in English, and therefore cannot be accurately translated into Chinese. Please provide more context or information.

#include <stdio.h>

void c(void)
{
  printf("c
");
}

主要.c:

#include <stdio.h>

void a(void);
void b(void);

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

建造和运行记录:

$ 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




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签