Compiling gcc 4.6.1 on Mac OSX Lion
Posted on September 20, 2011 by Sol

There is an update for this tutorial at http://solarianprogrammer.com/2013/06/11/compiling-gcc-mac-os-x/.

In this tutorial I will show you how to compile from source and install the last stable version of gcc on your Mac OSX Lion computer. This is for the people interested in running the last gcc without messing with the default gcc installation from Apple, which at the time of this writing is gcc 4.2.1.

In order to compile gcc from sources you will need a working installation of ... well gcc ... I will suppose you have already installed the last version of Xcode from Apple website. Xcode 4 is free for OSX Lion users.

We will begin by downloading the last stable release of gcc from the GNU website, so go to: ftp://ftp.gnu.org/gnu/gcc/gcc-4.6.1/ and select gcc-4.6.1.tar.gz, the archive is about 89.1 MB, I've saved the archive in my Downloads folder.

You will also need three other libraries for a successful build of gcc: mpc, mpfr and gmp, all of them can be downloaded from ftp://gcc.gnu.org/pub/gcc/infrastructure/. Just pick the last versions: gmp-4.3.2.tar.bz2, mpc-0.8.1.tar.gz and mpfr-2.4.2.tar.bz2, also save them in your Downloads folder.

Open a Terminal (personally I prefer iTerm2 but the Terminal provided with your Mac will do just fine). Starting with OSX 1.7 (Lion) Apple the gcc command will invoke the LLVM compilers and not the GNU compilers. This can be seen by checking the gcc version in Terminal:

 1 2 gcc --V i686-apple-darwin11-llvm-gcc-4.2: no input files 

Compiling gcc-4.6.1 with llvm-gcc will fail, we need to temporary modify the default compilers and linker to use the GNU version, paste the following lines in your Terminal:

 1 2 3 4 export CXX=/usr/bin/g++-4.2 export CC=/usr/bin/gcc-4.2 export CPP=/usr/bin/cpp-4.2 export LD=/usr/bin/ld 

We will start by compiling the gmp library, double click on gmp-4.3.2.tar.bz2, this will extract the entire library in your Downloads folder and go to the gmp folder:

 1 2 cd Downloads cd gmp* 

Create a new folder named build in which the compiler will save the compiled library:

 1 2 mkdir build cd build 

And now the fun part ... write in your Terminal:

 1 ../configure --prefix=$HOME/my_gcc  this will prepare the package for installation in your User_Name/my_gcc folder, you can change the name my_gcc for something more meaningful to you. If you see no error message we can actually compile the gmp library:  1 make  In a few minutes you will have a compiled gmp library. On the gmp website it is recommended to check if the compilation was OK by running some tests, we will do that with:  1 make check  If you see no error message ... congratulations, we are ready to install the library in my_gcc folder:  1 make install  We will do the same steps for MPFR now, double click on mpfr-2.4.2.tar.bz2 to extract the archive and navigate in the extracted library:  1 2 3 4 5 cd .. cd .. cd mpfr* mkdir build cd build  Configuration phase:  1 ../configure --prefix=$HOME/my_gcc --with-gmp=$HOME/my_gcc  The second parameter will just inform the configure app that gmp is already installed in my_gcc. After the configure phase has finished make and install the library:  1 2 make make install  Now, for MPC, extract the archive and go to the mpc folder: [codelang=”console” ] cd .. cd .. cd mpc* mkdir build cd build ../configure –prefix=$HOME/my_gcc –with-gmp=$HOME/my_gcc –with-mpfr=$HOME/my_gcc make make install [/code]

At this time you should have finished to build and install the prerequistes for gcc, let's compile the gcc package, be prepared that this could take more than one hour on some machines ... Double click on gcc-4.6.1.tar.gz and go in the newly extracted folder:

 1 2 3 4 5 6 cd .. cd .. cd gcc* mkdir build cd build ../configure --prefix=$HOME/my_gcc --enable-checking=release --with-gmp=$HOME/my_gcc --with-mpfr=$HOME/my_gcc --with-mpc=$HOME/my_gcc --program-prefix=my- 

The above command instructs the configure app where we have installed gmp, mpfr and mpc; also it tells to add a prefix to all the resulting executable programs, so for example if you will invoke gcc 4.6.1 you will write my-gcc, the gcc command will invoke Apple's built compiler gcc 4.2.1. If you prefer to write gcc-4.6.1 instead of my-gcc, just change the above line in:

 1 ../configure --prefix=$HOME/my_gcc --enable-checking=release --with-gmp=$HOME/my_gcc --with-mpfr=$HOME/my_gcc --with-mpc=$HOME/my_gcc --program-suffix=-4.6.1 

If you are interested in building only a limited set of languages, you could also specify this option in the configure line. For example if you need only C, C++ and Fortran just append this to the above line:

 1 --enable-languages=c,c++,fortran 

And now the final touches:

 1 make 

If your computer has a multicore processor, as is the case with any modern machine today, you can use them to do the make phase in parallel, just use the next command instead of the above make line (use a number slightly larger than the number of your cores, use for e.g. 4 for a two-cores machine):

 1 make -j 4 

Grab a coffee, maybe a book, and wait ... this should take approximately, depending on your computer configuration, about an hour ... or more ... and 2GB of your disk space for the build folder.

Install the compiled gcc in my_gcc:

 1 make install 

Now, you can keep the new compiler completely isolate from your Apple gcc compiler and, when you need to use it, just modify temporary your path by writing in Terminal:

 1 export PATH=$HOME/my_gcc/bin:$PATH 

Now, you should be able to invoke any of the newly compiled compilers C, C++, Java, Fortran ..., invoking g++ is as simple as writing in your Terminal:

 1 my-g++ test.cpp -o test 

Let's check if g++-4.6.1 can compile some C++11 specifics, in your favorite text editor copy and save this test program (I will suppose you will save the file in your Home directory):

  1 2 3 4 5 6 7 8 9 10 //Program to test the new C++11 lambda syntax #include ; using namespace std; int main() { cout << [](int m, int n) { return m + n; } (2, 4) << endl; return(0); } 

Compiling and running the above executable will return ... 6:

 1 2 3 my-g++ -std=c++0x tst_lambda.cpp -o tst_lambda ./tst_lambda 6