Friday, October 8, 2010

Integrating GCC 4.4 to Xcode (MacOS/Snow Leopard)

For some reasons Apple does not update support for latest versions of gcc in Xcode and this article describes dirty way to make it possible.

For uninitiated, Xcode is IDE on mac os, and latest Xcode version 3.2.4 (on the moment of writing) support only gcc 4.0 and 4.2 versions. Given that current stable version of gcc is 4.5.0 it seems there is some lag. In spare time I do some development using gnu C++ and despite being big fun of  command line and make specifically I decided integrate gcc 4.4.4 (that what I am currently using) into Xcode.

After some googling I found few ideas but no complete solution. For instance gcc_select from macports which support to switch compilers is quite useless for Xcode. After some investigation and trying I was able successfully integrate gcc 4.4 (to compile, link, debug, etc) into Xcode.

Xcode uses plugin system to integrate different functionality such as support for compilers. Each compiler support specific set of options and relevant .plist file describes their usage.

My changes are relevant for gcc 4.4.4 but the process would be the same for other version:

In /Developer/Library/Xcode/Plug-ins I copied folder GCC 4.2.xcplugin to  GCC 4.4.xcplugin, then in that directory modified com.apple.xcode.compilers.gcc.4_4  entry in Contents/Info.plist. In Contents/Resources renamed GCC 4.2.xcspec to GCC 4.4.xcspec plus renamed .strings file in English and Japanese subfolder.


So new directory structure now is:

$ ls -R GCC\ 4.4.xcplugin/
Contents

GCC 4.4.xcplugin//Contents:
Info.plist Resources version.plist

GCC 4.4.xcplugin//Contents/Resources:
1.txt English.lproj GCC 4.4.xcspec GCC 4.4.xcspec.old Japanese.lproj

GCC 4.4.xcplugin//Contents/Resources/English.lproj:
GCC 4.4.strings InfoPlist.strings

GCC 4.4.xcplugin//Contents/Resources/Japanese.lproj:
GCC 4.4.strings InfoPlist.strings

Then I renamed all mentions of 4.2 to 4.4 in GCC 4.4.xcspec:
Identifier = "com.apple.compilers.gcc.4_4";
Description = "GNU C/C++ Compiler 4.4";
ExecPath = "$(PLATFORM_DEVELOPER_BIN_DIR)/g++-mp-4.4";

Except the following:

    RequiredComponents = (
        "com.apple.compilers.gcc.headers.4_2",
    );


Also I created symlink for g++-mp-4.4 in /Developer/usr/bin so it can be reachable through ExecPath.


After that Xcode in Edit Project Settings/Build should have a choice for gcc 4.4 compiler but my program still did not compile due to some errors - apparently gcc 4.4 doesn't support all options of gcc 4.2 that Xcode tried to feed it to.

The errors I got:

cc1plus: error: unrecognized command line option "-arch"
cc1plus: error: unrecognized command line option "-fpascal-strings"
cc1plus: error: unrecognized command line option "-fasm-blocks"
cc1plus: error: ..fetcher-own-target-headers.hmap: not a directory
cc1plus: error: ..fetcher-all-target-headers.hmap: not a directory


So to disable options gcc 4.4 is not familiar with I changed GCC 4.4.xcspec: 
disabled USE_HEADERMAP, GCC_CW_ASM_SYNTAX, GCC_ENABLE_PASCAL_STRINGS  - change default to No, and removed mentioning of -arch (under Options/CommandLineArgs/<<otherwise>>).

Now all works as planned, compilation/linking/debugging - no issues.