How To: use gcc 4.2 with Xcode 4.2

Tue. November 8, 2011
Categories: Compilers, gcc-4.2, iOS, llvm, Xcode, Xcode-4.2
Tags: , , , ,

After 3 days of trial and error, I have finally found a way to enable compiling with gcc 4.2 in xcode 4.2 that works on the iOS Simulator and iOS Devices (including 5.0). If you are looking to use GCC 4.2 in Xcode 4.2 for non-iOS Apps, see this post. This is all done via the command line, so when you see lines starting with: $, you need to open up Terminal (or better yet, download and use iTerm) and type the text that starts after the $ to run that command.

No files nor directories are removed nor deleted in this process, so it is easy to undo if you need to compile with LLVM in the future.

  1. Download – but do not install yetxcode_4.1_for_lion.dmg or xcode_4.1_for_snow_leopard.dmg

  2. Now, follow these steps to install Xcode 4.1 into /Developer-4.1:

    1. Backup the working /Developer directory (where Xcode 4.2 is installed):

      $ sudo mv /Developer /Developer-4.2
    2. Run the Xcode 4.1 installer using the default install location (/Developer)

    3. Move the new Xcode 4.1 installation to /Developer-4.1:

      $ sudo mv /Developer /Developer-4.1
    4. Move the Xcode 4.2 developer directory back to /Developer:

      $ sudo mv /Developer-4.2 /Developer
  3. Edit the Xcode 4.2 GCC 4.2.xcspec file to get gcc 4.2 to show in the list of compiler options [1]:

    $ sudo vi /Developer/Library/Xcode/PrivatePlugIns/Xcode3Core.ideplugin/Contents/SharedSupport/Developer/Library/Xcode/Plug-ins/GCC\ 4.2.xcplugin/Contents/Resources/GCC\ 4.2.xcspec
    • Change lines 41 and 42 from this:

      ShowInCompilerSelectionPopup = NO;

      IsNoLongerSupported = YES;
    • To This:

      ShowInCompilerSelectionPopup = YES;

      IsNoLongerSupported = NO;
  4. Backup the Xcode 4.2 iOS/Simulator Framework usr directories:

    $ sudo mv /Developer/Platforms/iPhoneOS.platform/Developer/usr /Developer/Platforms/iPhoneOS.platform/Developer/usr.backup

    $ sudo mv /Developer/Platforms/iPhoneSimulator.platform/Developer/usr /Developer/Platforms/iPhoneSimulator.platform/Developer/usr.backup
  5. Copy Xcode 4.1 iOS/Simulator Framework usr directories to Xcode 4.2:

    $ sudo cp -r /Developer-4.1/Platforms/iPhoneOS.platform/Developer/usr /Developer/Platforms/iPhoneOS.platform/Developer/usr

    $ sudo cp -r /Developer-4.1/usr /Developer/Platforms/iPhoneSimulator.platform/Developer/usr
  6. Copy the gcc and info iOS SDK library directories from Xcode 4.1 to Xcode 4.2 [2]:

    $ sudo cp -r /Developer-4.1/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib/gcc /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/lib/gcc

    $ sudo cp -r /Developer-4.1/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib/info /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/lib/info
  7. Compile using gcc-4.2!

    Compile using gcc-4.2 in xcode 4.2

[1] If opening from a command line (using something like vi, emacs, nano, etc) make sure to either enclose the path in quotes "/long path/with spaces/in it/file.xcspec" or escape the spaces /some/long\ path/with\ spaces/in\ it/file.xcspec

[2] This is necessary because the iPhoneOS.platform SDK has its own seperate /usr/lib directories but the iPhoneSimulator.platform SDK does not

11 Responses to “How To: use gcc 4.2 with Xcode 4.2”

  1. alexbutenko says:

    First of all, thanks for your great work. I’ve completed all steps, that you’ve described and I’ve actually got GCC 4.2 compiler now. But I can’t compile anything using it, even sample Single View Project. In every file I get: GCC 4.2 error. Error writing to -: Broken pipe. Do you have any ideas about this issue? Thanks in advance.

  2. Followme says:

    Hi, everyone.
    Hi, HozBlog.
    [How to use gcc 4.2 with Xcode 4.2] seems like the best article to use gcc in Xcode 4.2.
    I’ve worked all steps and got GCC 4.2 compiler option.
    How nice it is……..

    But when I compile simple things using GCC, [Error writing to -: Broken pipe] happens to me.
    I have been wrestling with this error message and I have not found the solution out yet.
    Some one who knows it, Please tell me about the solution.

    I am looking forward to hearing from you.

    • root says:

      There is a problem with the connection from xcode/gbd to your device. Double check your ~/.gdbinit file , make sure your device is enabled for that provisioning profile, and also make sure your iTunes and Xcode are the newest versions. I don’t know if this will work for jailbroken iphones either, but I have not tried debugging through Xcode via gdb on a jb device yet.

  3. therekz says:

    I am getting the following build error when starting a new project after doing the steps above:

    Parse Issue Group
    /Developer/Test/Test/AppDelegate.h:15:12: error: unknown property attribute ‘strong’ [1]

    /Developer/Test/Test/AppDelegate.h:15:12: error: unknown property attribute ‘strong’ [1]

    /Developer/Test/Test/AppDelegate.h:17:12: error: unknown property attribute ‘strong’ [1]

    /Developer/Test/Test/AppDelegate.h:17:12: error: unknown property attribute ‘strong’ [1]

    /Developer/Test/Test/main.m:15:5: error: unexpected ‘@’ in program [1]

    • root says:

      To fix this you would want to change the keyword strong to retain in your AppDelegate header file. Then, in your main.m, change this:

      @autoreleasepool {

      To this:

      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      [pool drain];

      These changes are necessary because this new @autorelease and strong syntax only works with the llvm compiler and is not valid according to gcc-4.2. Xcode 4.2 templates are all written for the default llvm compiler, but they can be modified. You can check out my post on customizing Xcode template files for more details on that.

  4. gcurkov says:

    Nice work, thank you. I noticed you provided a direct download link to xcode 4.1, do you happen to have a link to xcode 4.0.1 for lion?


Leave a Reply