24

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:

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.

zoul
  • 102,279
  • 44
  • 260
  • 354
  • Are you linking using LLVM which supports ARC? – Krizz Jan 06 '12 at 10:37
  • first though: the toolchain's probably just added a library to link when ARC is enabled by the main project. if you can't locate it in the transcripts, then you may be able to link to it by compiling one source with ARC. – justin Jan 06 '12 at 10:41
  • 1
    That’s a great idea, @Justin! The trick with a single ARC-enabled file did not work, but I managed to find the right argument for the linker to include the library and it seems to work. Can you please that as an answer? A simple one will do, I’ll edit it to add the details. – zoul Jan 06 '12 at 10:48
  • @zoul great! answer added. edit away =) – justin Jan 06 '12 at 11:23

2 Answers2

30

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.

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.

zoul
  • 102,279
  • 44
  • 260
  • 354
justin
  • 104,054
  • 14
  • 179
  • 226
  • Thanks a lot, this answer was a life-saver after a whole afternoon trying to figure this out! – AliSoftware Mar 21 '12 at 17:02
  • 2
    This no longer appears to work as of Xcode 4.3.2. `-fobjc-arc` seems to be an invalid flag for lib tool. ("unknown option character `f' in: -fobjc-arc") – Luke Redpath Mar 26 '12 at 12:24
  • 3
    I can confirm this is working in Xcode 4.3.2. I just did it, and the compiler didn't complain about anything when building for the simulator. – csotiriou May 12 '12 at 15:04
5

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.

This appears to be a known issue although the only thread I can find on this with 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:

${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!

zoul
  • 102,279
  • 44
  • 260
  • 354
Luke Redpath
  • 10,474
  • 3
  • 27
  • 26
  • Upvoting, since this is a useful addition. Did not test yet. – zoul Mar 28 '12 at 14:02
  • 1
    DEVROOT doesn't appear to work for me. I used: `$(PLATFORM_DEVELOPER_USR_DIR)/lib/arc` in the library search paths and a device-specific linker flag for `-larclite_iphoneos` – Jim Dovey Apr 12 '12 at 19:56
  • Useful addition, but I have yet to find the reason why just adding -fobjc-arc in the linker flags still works for me in Xcode 4.3.2 – csotiriou May 12 '12 at 18:15