Setting GCC 4.2 as the default compiler on Mac OS X Leopard

I'm sure there must be a way to do this. As you are probably aware the latest versions of Xcode (and in fact I think all versions of Xcode) on Leopard come with GCC 4.0.1 and GCC 4.2. GCC 4.0.1 is the default system compiler while GCC 4.2 is an optional compiler you can set in the Xcode project settings.

Does anyone know how to set GCC 4.2 as the default compiler for all options? Preferably command line use as well as configure scripts still use GCC 4.0.1 rather than GCC 4.2 no matter what I do in Xcode. I'm assuming it is simply a case of changing a path variable or some such but I am stumped on this one.

Any help is appreciated. Thanks.


Solution 1:

Command line usage for all configure scripts:

  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++

Make a record of the current link targets, so you can restore them if you want to.

If you don't want to change the system wide settings, add a directory into PATH before /usr/bin (say, $HOME/bin), and make the symlinks there

I haven't tested whether this affects Xcode projects, since I don't use Xcode (only command line).

Solution 2:

In the Project or Target Info Window set the build setting "C/C++ compiler version" (GCC_VERSION).

Or in the Target Info Window you can change the "System C rule" to your favorite GCC version.

Update: Regarding the command line I would leave to Leopard the decision of what should be the default compiler. If you want to use a different compiler with tools like Autotools configure you had better to define the CC variable.

CC=gcc-4.2 ./configure

or

export CC=gcc-4.2

Solution 3:

Since neither Apple nor Darwin Ports have the gcc_select program to change the default version of the System compiler (as exists on GNU/Linux), I would like to be on the safe side with XCode (and the rest of the system) and would recommend to leave the symbolic links as they are and instead setup environment variables that overrides which version of GCC to use.

In my .profile file I have the following

export CC=/usr/bin/gcc-4.2
export CPP=/usr/bin/cpp-4.2
export CXX=/usr/bin/g++-4.2 

And I successfully compiled the following libraries with GCC 4.2 from source.

  • OpenSSL
  • libjpeg
  • libpng
  • zlib
  • gst

However... I could not get Boost 1.39 to acknowledge the environment variables, so to compile Boost with GCC 4.2 I needed to change the symbolic links in /usr/bin/ so they pointed to gcc v4.2

After the long while the Boost libraries were finished compiling with GCC 4.2 I restored the symbolic links back to the original System version gcc-4.0.

Solution 4:

Im my experience (limited), changing CC in .profile does not change Lion's (10.7.2) defaulting to i686-apple-darwin11-llvm-gcc-4.2. I wonder if this has anything to do with Apple's own sym linking: a partial: ls -la /usr/bin | grep .*gcc.* :

lrwxr-xr-x     1 root   wheel        12 25 oct 19:31 cc -> llvm-gcc-4.2
lrwxr-xr-x     1 root   wheel        12 25 oct 19:31 gcc -> llvm-gcc-4.2
lrwxr-xr-x     1 root   admin        32 25 oct 19:31 llvm-gcc-4.2 -> ../llvm-gcc-4.2/bin/llvm-gcc-4.2

I am wary about breaking these and adding my own to usr/bin/gcc-4.2 per Martin v. Löwis's answer.

Solution 5:

Since I need to build things where CC env is ignored and I end up switching often, I wrote a simple minded gcc_select in Python. Thought I may as well post it here. Invoke it with arg either 4.0 or 4.2 or no arg to see current symlinks. Would need modification if you have versions other than 4.0 and 4.2:

#!/usr/bin/python
import sys
import os

os.chdir('/usr/bin')

files = ['cc', 'gcc', 'c++', 'g++']

if '4.0' in sys.argv:
  ver = '4.0'
elif '4.2' in sys.argv:
  ver = '4.2'
else:
  print "Unknown gcc version.  Current setting:"
  os.system('ls -al %s' % ' '.join(files))
  sys.exit(1)

os.system('rm %s' % ' '.join(files))  
for f in files:
  os.system('ln -s %s-%s %s' % (f, ver, f))

print "Changed to gcc version %s" % ver