xcode 具有 ARC 支持的静态库链接到非 ARC 项目,导致链接器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8756418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Static library with ARC support linked to non-ARC project causing linker errors
提问by zoul
I have a non-ARC project that uses an ARC-enabled static library. This is a supported scenario, so that everything works fine. That is, until I run the code on a 4.x device, including the Simulator. In that case the code blows up with the following linker error:
我有一个使用启用 ARC 的静态库的非 ARC 项目。这是一个受支持的场景,因此一切正常。也就是说,直到我在 4.x 设备上运行代码,包括模拟器。在这种情况下,代码会因以下链接器错误而崩溃:
dyld: lazy symbol binding failed: Symbol not found: _objc_storeStrong
Referenced from: /Users/zoul/Library/Application Support/iPhone Simulator/4.3.2/Applications/…/Demo.app/Demo
Expected in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/System/Library/Frameworks/Foundation.framework/Foundation
This happens as soon as some of the ARC-enabled code attempts to call _objc_storeStrong
function, like in an init
method (self = [super init]
). Converting the main project to ARC solves the problem, but I'd like to know if there are other solutions.
一旦某些启用 ARC 的代码尝试调用_objc_storeStrong
函数(例如在init
方法 ( self = [super init]
) 中),就会发生这种情况。将主项目转换为ARC解决了问题,但我想知道是否还有其他解决方案。
回答by justin
I assumed that the toolchain may have added the necessary libraries to link to, in order for ARC to work properly. So the linker transcript may contain this piece of information. If the project of the app itself is not ARC-enabled, you may not get these by default, but you could still link to them by defining them explicitly.
我假设工具链可能已经添加了必要的库来链接,以便 ARC 正常工作。所以链接器转录本可能包含这条信息。如果应用程序本身的项目未启用 ARC,则默认情况下您可能无法获得这些,但您仍然可以通过明确定义它们来链接到它们。
Looking at the build transcript you can indeed find the appropriate linker flag there: it's called -fobjc-arc
(just as the related compiler flag). When you add this setting to Other Linker Flags, the linker will include the ARC library with the main build product and the code should run fine.
查看构建记录,您确实可以在那里找到合适的链接器标志:它被调用-fobjc-arc
(就像相关的编译器标志一样)。当您将此设置添加到其他链接器标志时,链接器将包含主构建产品的 ARC 库,并且代码应该可以正常运行。
回答by Luke Redpath
I'm adding a new answer to this as the previous accepted solution no longer appears to work with Xcode 4.3.2. I can only assume that the -fobjc-arc
linker flag was never supposed to be exposed and has now been removed.
我正在为此添加一个新答案,因为以前接受的解决方案似乎不再适用于 Xcode 4.3.2。我只能假设-fobjc-arc
链接器标志永远不应该被暴露,现在已经被删除。
This appears to be a known issue although the only thread I can find on thiswith somebody from Apple commenting on the devforums dates back to mid-2011. From that thread, it is suggested that manually linking the following file solves the issue:
这似乎是一个已知问题,尽管我能找到的唯一线程与 Apple 的某个人对 devforums 的评论可以追溯到 2011 年年中。从该线程,建议手动链接以下文件解决问题:
${DEVROOT}/Platforms/iPhoneOS.platform/Developer/usr/lib/arc/libarclite_iphoneos.a
This requires you to be compiling using the latest compiler/SDK though. I'm submitting this answer without testing, please upvote if it works, downvote if it doesn't!
不过,这要求您使用最新的编译器/SDK 进行编译。我在没有测试的情况下提交了这个答案,如果有效,请投票,如果无效,请投票!