Building gcc on MacOS X

Apple has never included a stock version of GCC with their development tools, but now with the current version of Xcode, they don’t even include a modified version.  Seeing as their plan going forward is to move entirely to Clang/LLVM, if you intend to use GCC on OS X, you’ll have to build it yourself.  It’s not a terribly difficult process, but it can be a bit tricky the first few times around, particularly when it comes to configuring the build.

Instructions follow.

1) Install Xcode.  You need an existing compiler and development tools to build GCC.  The current release is 4.2 (requires MacOS X Lion).  It’s a 1.8GB download from the Mac App store.

2) Install MacPorts.  MacPorts is a handy way for installing just about any widely used open source packages.

3) Install the many GCC dependencies using MacPorts.  These are:

  • gmp (multi-precision arithmetic package)
  • mpfr (multi-precision floating point package)
  • mpc (multi-precision complex arithmetic package)
  • ppl (Parma polyhedral library)
  • cloog (Chunky loop generator)

This can all be done with one command (didn’t I say MacPorts was handy):

$ sudo port -v install gmp mpfr libmpc cloog-ppl ppl

4) Get and decompress GCC.  For the current release (GCC 4.6.1), that’d be

$ curl -f http://ftp.gnu.org/gnu/gcc/gcc-4.6.1/gcc-4.6.1.tar.bz2 -o gcc-4.6.1.tar.bz2
$ tar -xjvf gcc-4.6.1

5) Configure the GCC build machinery.  You shouldn’t actually do this from the gcc directory (long story), so create a separate directory first to do the build in.  I enable only the languages I intend to use to speed up the build (in particular, Java can take forever to build).

$ mkdir build-gcc
$ cd build-gcc
$ ../gcc-4.6.1/configure –enable-languages=c,c++,objc,obj-c++ –disable-nls –with-ppl=/opt/local/ \
–with-gmp=/opt/local/ –with-cloog=/opt/local/ –with-mpfr=/opt/local/ –with-mpc=/opt/local/ –prefix=/usr/local/gcc

6) Build GCC.  This usually takes a while.  You don’t have to do a bootstrap, but it’s a good way to ensure that the compiler you’re building is more or less stable.  Bootstrapping involves using the system compiler to build gcc (stage 1), using the stage 1 gcc to build gcc (stage 2), and using the stage 2 gcc to build gcc (stage 3).  The stage 2 and 3 compilers are compared – they should be identical at the binary level as both are compiled with exactly the same version of gcc.  In any case, the command is simple enough.

$ make bootstrap-lean

7) Install GCC.

$ make install

8) You’re done.  You can either add /usr/local/gcc/bin to $PATH, or invoke gcc directly as /usr/local/gcc/bin/gcc .  The advantage of installing gcc in its own directory is that it can’t mess anything else up, and you can easily have multiple versions of the compiler accessible at a given time.

Comments are closed.