Living on the edge building gcc-4.7 on Mac OSX Lion
Posted on February 20, 2012 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 current development version of gcc on your Mac OSX Lion computer. The instructions from this tutorial were tested with Xcode 4.3 and Mac OSX Lion 10.7.3. Since this is still a development version, it is not recommended to use it in production, however you can use it to learn and test the newest additions to the compiler.

Having the last version of gcc on your Mac could be handy if you want to test some of the new C++11 capabilities that are present in gcc-4.7, like for e.g. lambdas (at the time of this writing clang++ does not have lambda) or multithread programming. It could also be useful if you want to play with the last Fortran standard (2003/2008) syntax from gfortran-4.7 like do concurrent.

If you prefer to use the stable version of gcc, 4.6.2, you can check the older version of this tutorial. Using gcc-4.6.2 will not allow you to compile C++11 code that uses the thread header on Mac OSX (however you can use clang++).

In order to compile gcc from sources you will need a working installation of … well gcc … Starting with Xcode 4.3 you have two options: install the full Xcode which can take a good chunk of your hard drive (this is the recommended option if you plan to develop for Apple Store or if you like to use an IDE); a second, more economical option, is to install only the Command Line Tools for Xcode which has about 164 MB at download and can take about 300 MB of space on your HDD. If you chose to install the Command Line Tools for Xcode you will have a full developer Unix environment (gcc, g++, make etc…) on your machine.

We will begin by downloading the last snapshot of gcc-4.7 from the GNU website, so go to: http://gcc.gnu.org/mirrors.html and select the last snapshot of gcc-4.7, which at the time of this writing is gcc-4.7-20120218.tar.bz2. 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. Use the above links and download the last versions for all of them: gmp-5.0.4.tar.bz2, mpc-0.9.tar.gz and mpfr-3.1.0.tar.gz, also save them in your Downloads folder.

Open a Terminal (personally I prefer iTerm2 but the Terminal provided with your Mac will work just fine). Starting with Xcode 4.3, Apple has removed the GNU version of gcc from their development tools, this was completely replaced with the LLVM compiler. 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 

We will start by compiling the gmp library, so double click on gmp-5.0.4.tar.bz2. This will extract the entire library in your Downloads 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. 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-3.1.0.tar.gz 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 is finished, we can make and install the library:  1 2 make make install  Now, for MPC, extract the archive and go to the mpc folder:  1 2 3 4 5 6 7 8 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 

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.7-20120218.tar.bz2 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.7 you will write my-gcc, the gcc command will invoke Apple's built compiler gcc 4.2. If you prefer to write gcc-4.7 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.7 

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 

Since I’m interested only in C, C++ and Fortran, this is the configure command I’ve used on my machine:

 1 ../configure --prefix=$HOME/my_gcc --enable-checking=release --with-gmp=$HOME/my_gcc --with-mpfr=$HOME/my_gcc --with-mpc=$HOME/my_gcc --enable-languages=c,c++,fortran --program-suffix=-4.7 

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-core 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 about 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's gcc compiler and, when you need to use it, just modify your path by writing in Terminal:

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

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

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

Remember to erase your *build* directories from Downloads if you want to recover some space.

Let's check if g++-4.7 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 lambda example will return ... 6:

 1 2 3 g++-4.7 -std=c++11 tst_lambda.cpp -o tst_lambda ./tst_lambda 6 

g++-4.7 is able to successfully compile C++11 threads on Mac OSX (this was not the case for the 4.6 series):

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 //Create a C++11 thread from the main program #include #include //This function will be called from a thread void call_from_thread() { std::cout << "Hello, World!" << std::endl; } int main() { //Launch a thread std::thread t1(call_from_thread); //Join the thread with the main thread t1.join(); return 0; } 

If you are a Fortran programmer, you can use some of the Fortran 2008 features like do concurrent with gfortran-4.7:

  1 2 3 4 5 6 7 8 9 10 11 12 integer,parameter::mm=100000 real::a(mm), b(mm) real::fact=0.5 ! initialize the arrays ! ... do concurrent (i = 1 : mm) a(i) = a(i) + b(i) enddo end 

The above code can be compiled with (supposing you’ve named it tst_concurrent_do.f90):

 1 2 gfortran-4.7 tst_concurrent_do.f90 -o tst_concurrent_do ./tst_concurrent_do 

Closing your Terminal session should clean your Path.

If you are interested in learning more about the new C++11 syntax I would recommend reading The C++ Programming Language by Bjarne Stroustrup.

or, Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper 2nd edition:

If you need to brush your Fortran knowledge a good book is Modern Fortran Explained by M. Metcalf, J. Reid and M. Cohen: