3

I need to use gcc 4.2 with Xcode 4.2 for some older code. How do i get it to show up. Xcode4.2 seems to have dropped support for it.

Thanks

chown
  • 51,908
  • 16
  • 134
  • 170
tech74
  • 1,609
  • 1
  • 20
  • 39

4 Answers4

4

Here is a way to enable compiling with gcc 4.2 in xcode 4.2. This is mostly done via command line so when you see lines starting with: [ 15:30 jon@MacBookPro / ]$, you need to open up Terminal.app and run the command that starts after the $.

No files or directories are removed or 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 yet - xcode_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)

    2. [ 15:30 jon@MacBookPro / ]$ sudo mv -v /Developer /Developer-4.2
      
  3. Run the Xcode 4.1 installer using the default install location (/Developer)

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

    [ 15:30 jon@MacBookPro / ]$ sudo mv -v /Developer /Developer-4.1
    
  5. Move the Xcode 4.2 developer directory back to /Developer:

    [ 15:30 jon@MacBookPro / ]$ sudo mv -v /Developer-4.2 /Developer
    
  • Edit the Xcode 4.2 GCC 4.2.xcspec file to get gcc 4.2 to show in the list of compiler options [1]:

    [ 15:30 jon@MacBookPro / ]$ sudo vi "/Developer/Library/Xcode/PrivatePlugIns/Xcode3Core.ideplugin/Contents/SharedSupport/Developer/Library/Xcode/Plug-ins/GCC 4.2 (Plausible Blocks).xcplugin/Contents/Resources/GCC 4.2.xcspec"
    
    • Change lines 41 and 42 from this:

      ShowInCompilerSelectionPopup = NO;
      IsNoLongerSupported = YES;
      
    • To This:

      ShowInCompilerSelectionPopup = YES; 
      IsNoLongerSupported = NO;
      
  • Backup the Xcode 4.2 iOS/Simulator Framework usr directories:

    [ 15:30 jon@MacBookPro / ]$ sudo mv -v /Developer/Platforms/iPhoneOS.platform/Developer/usr /Developer/Platforms/iPhoneOS.platform/Developer/usr.backup
    [ 15:30 jon@MacBookPro / ]$ sudo mv -v /Developer/Platforms/iPhoneSimulator.platform/Developer/usr /Developer/Platforms/iPhoneSimulator.platform/Developer/usr.backup
    
  • Copy Xcode 4.1 iOS/Simulator Framework usr directories to Xcode 4.2:

    [ 15:30 jon@MacBookPro / ]$ sudo cp -rv /Developer-4.1/Platforms/iPhoneOS.platform/Developer/usr /Developer/Platforms/iPhoneOS.platform/Developer/usr
    [ 15:30 jon@MacBookPro / ]$ sudo cp -rv /Developer-4.1/usr /Developer/Platforms/iPhoneSimulator.platform/Developer/usr
    
  • Copy the gcc and info iOS SDK library directories from Xcode 4.1 to Xcode 4.2 [2]:

    [ 15:30 jon@MacBookPro / ]$ sudo cp -rv /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
    [ 15:30 jon@MacBookPro / ]$ sudo cp -rv /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
    
  • Compile using gcc-4.2!

    Screenshot of CGG 4.2 in xcode 4.2

  • This is a blog post I've written with a little more info about this process. Feel free to leave a comment on the blog if you run into any issues or have any questions.


    [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

    chown
    • 51,908
    • 16
    • 134
    • 170
    • HI ,Thanks but I'm not getting gcc option under the Compiler options. I just have LLVM 3.0 and LLVM gcc 4.2. How to get the IDE to show the gcc compiler – tech74 Oct 16 '11 at 20:11
    • can't exec '/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2' (No such file or directory) – tech74 Oct 16 '11 at 22:23
    • Do I need to create a gcc-4.2 directory aswell, its just it wasn't on the list of steps. I have it set in the IDE which is good but when I try and build I get the above error – tech74 Oct 16 '11 at 22:25
    • unfortunately somewhere along the line i uninstalled Xcode so gcc4.2 is not on my machine so hence my problem. I think i will reinstall – tech74 Oct 16 '11 at 22:46
    • @tech74 Cool, good luck! Btw, `gcc-4.2` should still be installed in `/usr/bin/` – chown Oct 16 '11 at 23:02
    • Unfortunately I'm now getting this Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 failed with exit code 255 – tech74 Oct 16 '11 at 23:35
    • @tech74 Ah, seems its missing the i686 gcc too. symlink with: `ln -s /usr/bin/i686-apple-darwin11-gcc-4.2.1 /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/i686-apple-darwin11-gcc-4.2.1` – chown Oct 17 '11 at 01:59
    • Manually entering command line from Xcode build log gives me gcc-4.2: error trying to exec '/usr/bin/arm-apple-darwin10-gcc-4.2.1': execvp: No such file or directory – tech74 Oct 17 '11 at 06:37
    • I don't have this on my machine, not sure why not. I installed Xcode 4.02 and then Xcode 4.2 ontop – tech74 Oct 17 '11 at 06:44
    • It works on the iPhone 5.0 simulator!!! just need to sort out the device(arm) version – tech74 Oct 17 '11 at 06:56
    • @tech74 I spent a few hours last night and this morning figuring out exactly what is required to compile for gcc4.2 for the Simulator and for an iOS Device. I've updated the steps in my answer; it should be pretty straight-forward but let me know if you run into any issues. – chown Oct 17 '11 at 20:28
    • Hi, i believe this is working its just because I have some assembly code in some of my libs I haven't got it all built. I am getting this error – tech74 Oct 20 '11 at 10:53
    • /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/../libexec/gcc/arm-apple-darwin10/4.2.1/as: assembler (/Developer/Platforms/iPhoneOS.platform/Developer/usr/libexec/gcc/arm-apple-darwin10/4.2.1/../libexec/gcc/darwin/arm/as or /Developer/Platforms/iPhoneOS.platform/Developer/usr/libexec/gcc/arm-apple-darwin10/4.2.1/../local/libexec/gcc/darwin/arm/as) for architecture arm not installed /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/../libexec/gcc/arm-apple-darwin10/4.2.1/as: no assemblers installed – tech74 Oct 20 '11 at 10:55
    • If youve got xcode 4.1 installed still I think you can fix that by copying `/Developer-4.1/Platforms/iPhoneOS.platform/Developer/usr/libexec` to `/Developer/Platforms/iPhoneOS.platform/Developer/usr/libexec` – chown Oct 20 '11 at 11:59
    • Yes it worked, Brilliant. I've built 700k lines of source consisting of multiple libs, everything seems to be running OK. Many many thanks – tech74 Oct 20 '11 at 15:03
    1

    Thanks chown for sharing this! I struggled around half a day, but at the end it turned out, that I only needed a few steps to achieve my goal:

    For everyone that just needs GCC-4.2 to compile apps (no iOS stuff) it's pretty easy: You don't need to install anything just follow these few steps:

    1. EDIT: If you don't have GCC 4.2 installed already (from an earlier version of Xcode, or manual install), follow the instruciotns from Sean DeNigris in the comments!

    2. Follow step 3 from the original description:
      Edit the Xcode 4.2 GCC 4.2.xcspec file to get gcc 4.2 to show in the list of compiler options.

    3. Make Xcode find the compiler again:

      ln -s /usr/bin/gcc-4.2 /Developer/usr/bin/gcc-4.2

    4. Also make the linker available:

      ln -s /usr/bin/g++-4.2 /Developer/usr/bin/g++-4.2

    This works perfectly for me! But as I said it's just for non iOS products!

    v01pe
    • 1,096
    • 2
    • 11
    • 19
    • This got the option to appear, but the executable files you mentioned (e.g. /Developer/usr/bin/gcc-4.2) do not exist. Did you upgrade from 4.1 to 4.2 because they don't seem to be included in a fresh 4.2 install? – Sean DeNigris Jan 11 '12 at 00:32
    • 1
      Apparently, for fresh installs of Xocde 4.2 (i.e. not upgrading from 4.1), you must manually install gcc; e.g. "sudo port install apple-gcc42" – Sean DeNigris Jan 11 '12 at 01:16
    • @SeanDeNigris: exactly, I started working with Xcode at version 3.2.4 and kept updating since then. The project from back then didn't like the LLVM GCC 4.2, the game kept crashing/ not having sound. – v01pe Jan 11 '12 at 19:23
    • If you're curious, I updated the project so it compiles and works with LLVM 4.2, because adding GCC 4.2 support to Xcode 4.3 is totally messed up and I have no idea how it could work :D – v01pe Feb 28 '12 at 14:21
    1

    So the full process for getting gcc 4.2 to work with a fresh install of Xcode 4.2 (non-iOS) is:

    1. Install gcc 4.2

      sudo port install apple-gcc42
      
    2. Tweak Xcode so that gcc 4.2 appears as a compiler option, by editing the Xcode 4.2 GCC 4.2.xcspec file to get gcc 4.2 to show in the list of compiler options:

      1. Open the xcspec file for editing:

        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"
        
      2. Change lines 41 and 42 from this:

        ShowInCompilerSelectionPopup = NO;
        IsNoLongerSupported = YES;
        

        To This:

        ShowInCompilerSelectionPopup = YES;
        IsNoLongerSupported = NO;
        
    3. Link the gcc 4.2 binary to the location that Xcode expects:

      sudo ln -s /opt/local/bin/gcc-apple-4.2 /Developer/usr/bin/gcc-4.2
      sudo ln -s /opt/local/bin/g++-apple-4.2 /Developer/usr/bin/g++-4.2
      

    I explained the problem and workaround in this blog post, which condenses the information I found. Feel free to comment.

    Sean DeNigris
    • 6,306
    • 1
    • 31
    • 37
    0

    Simple and userful solution https://github.com/kennethreitz/osx-gcc-installer/ It automates the lists of the declaration on the stackoverflow

    alexmorhun
    • 1,903
    • 2
    • 18
    • 23