26

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.

Cromulent
  • 3,788
  • 4
  • 31
  • 41

6 Answers6

21

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).

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
  • This used to be handled by the `gcc_select` script, but it seems that Leopard / Xcode 3.1 doesn't provide it anymore? I haven't used OS X in years, so I'm a bit out of touch... – ephemient Jul 22 '09 at 21:00
  • Thank you, that fixed the issue. Just as an aside if anyone else is planning on doing this, I highly recommend putting the new symlinks in a different directory so that if you ever want to revert back to your original settings you just have to delete the symlinks or move them to a different directory. – Cromulent Jul 22 '09 at 23:28
  • Martin, a related question, how can you tell setup.py / pydistutils.cfg which CC and which -arch to use ? For example, I want gcc-4.2 -arch ppc only (because my gcc-4.2 has no /usr/bin/i686-apple-darwin8-g++-4.2.1). Thanks – denis Jul 27 '09 at 15:51
  • @Denis: They use the CC in the installed Makefile (distutils.sysconfig.get_makefile_filename()). In principle, setting CC and CXX environment variables should override that. – Martin v. Löwis Jul 27 '09 at 20:31
  • It worked on OS X 10.10.3 (six years into the future! you'd think this wouldn't be an issue anymore). – Parzival May 15 '15 at 23:57
  • I would strongly recommend llDan 's answer; that more flexible – juanmirocks Nov 30 '15 at 12:04
18

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
IlDan
  • 6,851
  • 3
  • 33
  • 34
  • Unfortunately I already know about those, but they are per project. I am talking about when you invoke GCC from the commmand line. No matter what I set the rules for in Xcode it will always use GCC 4.0.1 from the command line. – Cromulent Jul 22 '09 at 14:00
  • my fault sorry. I'm updating it – IlDan Jul 22 '09 at 14:24
  • 6
    I find 'export CC=gcc-X.Y' more practical (and *nix-like) when compared with the rm/ln -s solution. Easy to revert if something goes wrong, easy to setup user/system-wide, it only requires one to edit .bash_profile (or your favorite shell rc). – lsdr Aug 05 '09 at 12:13
16

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
user156592
  • 169
  • 2
3

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.

Centzon
  • 166
  • 8
2

I might be wrong, but I thought that was what Xcode-select was for?

xcode-select --switch /path_to_tool_suite

As I said, I'm not 100% on this, but I believe it will set the default for all programs including Terminal and other apps that calls into the OS to use a compiler.

2

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
Stephan Deibel
  • 126
  • 1
  • 2