且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么 XCode 4.2 中的 Three20 依赖项不再需要 -force_load?

更新时间:2023-02-01 17:20:12

在构建静态库(如 iOS 所需)时,您会遇到的问题之一是如何在该库中包含类别中的符号,以便它们可由应用程序重新使用.正如 Dave Dribin 在他的文章 此处.

When building a static library (as required for iOS), one of the issues you'll encounter is how to include symbols from categories in that library so they're usable by an application. The linker flag -ObjC should pull in enough information to include categories in these built frameworks, as Dave Dribin describes in his article here.

然而,在 iPhone OS 2.0 和 3.0 之间,这停止正常工作.正如我在这个答案中提到的,我们在Core Plot框架中遇到了这个问题,发现需要添加-all_load链接器标志才能让框架正常工作.Apple 自己发布了 技术问答 QA1490,其中提到了这一点问题:

However, between iPhone OS 2.0 and 3.0, this stopped working right. As I mentioned in this answer, we encountered this problem in the Core Plot framework and found that we needed to add the -all_load linker flag to make the framework work right. Apple themselves posted Technical Q&A QA1490 which mentions this issue:

对于 64 位和 iPhone OS 应用程序,存在链接器错误防止 -ObjC 从静态库加载对象文件只包含类别而没有类别.解决方法是使用-all_load 或 -force_load 标志.

For 64-bit and iPhone OS applications, there is a linker bug that prevents -ObjC from loading objects files from static libraries that contain only categories and no classes. The workaround is to use the -all_load or -force_load flags.

-all_load 强制链接器从它看到的每个存档中加载所有目标文件,即使是那些没有 Objective-C 代码的文件.-force_load 是在 Xcode 3.2 及更高版本中可用.它允许更细粒度的控制存档加载.每个 -force_load 选项后必须跟一个路径一个存档,并且该存档中的每个目标文件都将被加载.

-all_load forces the linker to load all object files from every archive it sees, even those without Objective-C code. -force_load is available in Xcode 3.2 and later. It allows finer grain control of archive loading. Each -force_load option must be followed by a path to an archive, and every object file in that archive will be loaded.

不幸的是,这样做的副作用是从多个库中链接的重复符号可能会导致您遇到的错误.

Unfortunately, the side effect of this is that duplicate symbols linked in from multiple libraries can cause errors like the ones you experienced.

我提交了一份关于此的错误报告(早在 2009 年),现在 Xcode 中使用的最新版本的 LLVM 似乎不再受此链接器错误的影响.我尝试将 -ObjC 与 Core Plot iOS 静态库目标一起使用,现在工作正常.这是个好消息.

I filed a bug report about this (back in 2009), and it appears that the latest version of LLVM now used in Xcode no longer suffers from this linker bug. I tried just using -ObjC with the Core Plot iOS static library target and it works fine now. That's welcome news.