Saturday, April 5, 2014

Adding ARC Code to a Project That Also Compiles for i386

Jeff Johnson:

The ARCHS build setting for your framework includes both i386 and x86_64. The first thing you'll want to do is to put your ARC code in files that aren't used by your 32-bit apps. Next, you'll want to wrap your ARC code files with #if __LP64__ to conditionally compile the code for 64-bit. As a consequence, the files will simply be empty when compiling i386. Finally, ARC can be enabled for individual files with the -fobjc-arc compiler flag. In Xcode, you can set per-file flags under Build Phases in the Compile Sources build phase. The catch, for “fat” or “universal” builds that are both 32- and 64-bit, is that you cannot set your per-file flags to -fobjc-arc. Why not? Because the per-file compiler flag applies to every architecture, but -fobjc-arc is an invalid flag for i386. So your build will die.

The trick to enabling ARC for specific files in your framework is to use a per-architecture build setting. Create a User-Defined build setting, something like MY_FRAMEWORK_USE_ARC. Make the build setting empty. Then create a per-architecture variant of MY_FRAMEWORK_USE_ARC for Intel 64-bit, and set that variant to -fobjc-arc. You'll now be able to use $MY_FRAMEWORK_USE_ARC as the per-file flag in your build phase, and the flag will have the appropriate definition as each architecture is built.

Also, when you’re in Xcode’s Compile Sources view, you can select multiple files and press Return to batch-edit the Compiler Flags.

1 Comment RSS · Twitter

[...] This is probably the “right” solution, but it’s kind of a pain. [...]

Leave a Comment