GCC on OS X Lion with Xcode 4.3.1

19,469

Solution 1

Open XCode go to preferences under downloads install command line tools

Solution 2

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.

Solution 3

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.

Share:
19,469
SimonBS
Author by

SimonBS

Updated on September 18, 2022

Comments

  • SimonBS
    SimonBS over 1 year

    After installing Xcode 4.3.1 on OS X Lion, I have lost my GCC compiler. I need this for my study. Does anyone know how I will install this? All I have been able to find was people suggesting to install Xcode 4.3.1 but with the latest Xcode build, Apple has moved the GCC compiler inside the Xcode.app and it doesn't seem to be the same version of the GCC compiler as the one bundled with previous versions of Xcode.

  • Scott C Wilson
    Scott C Wilson about 12 years
    One suggestion: instead of removing the old binaries (line 2 of the script), perhaps just put them in an archive directory under /usr/bin). That way if you need them you can still invoke them by absolute path.
  • JerseyGirl1201
    JerseyGirl1201 about 12 years
    Good suggestion. Do with them as you will, even: mv cc cc.orig && mv gcc gcc.orig or some such thing.
  • Chance
    Chance about 12 years
    I really wish I had found your solution a few days ago. I fought it for two days straight. I'd +20 if I could.
  • SimonBS
    SimonBS about 12 years
    This was definitely the easiest way to do it. Thanks.