GCC on OS X Lion with Xcode 4.3.1

Open XCode go to preferences under downloads install command line tools


There have been a couple of blog posts about how to do this recently, in the context of trying to compile Ruby 1.8.7 (mine and others). The short answer:

Install Command Line Tools (in Xcode: Preferences > Downloads > Components; or you can go to Apple and download them separately if you don't have/want Xcode installed) - this installs a gcc command, but it's actually clang Install Homebrew (instructions), a package manager for OS X

Then, install the 'dupes' homebrew repository and from it the genuine GCC 4.2 compiler:

brew update
brew tap homebrew/homebrew-dupes
brew install apple-gcc42

/usr/bin/gcc will still be i686-apple-darwin11-llvm-gcc-4.2, but /usr/local/bin/gcc-4.2 will be i686-apple-darwin11-gcc-4.2.1 (and associated tools, g++-4.2 and so forth, also in /usr/local/bin)

Then, you can use whatever mechanism your build process uses (for instance, setting the CC and CXX environment variables appropriately) to select those compilers rather than the clang versions.


I don't use Xcode 4.3 for actual Xcode Projects, only for command line source compilation, and had the same issue. You can bring back your gcc and cc commands with the below commands from terminal:

cd /usr/bin
rm cc gcc c++ g++
ln -s gcc-4.2 cc
ln -s gcc-4.2 gcc
ln -s c++-4.2 c++
ln -s g++-4.2 g++

This will make system wide changes, so be sure that's what you want before doing it.

For these commands to work you'll also need the Command Line Tools for Xcode to be installed, if you don't already have them installed. The Tools can be installed through Xcode's downloads preference pane.