Compiling gcc-4.7.1 on Mac OSX Lion
Posted on July 21, 2012 by Sol

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

3 August 2012 update

You can use this tutorial for compiling gcc-4.7.1 on Mountain Lion too. I’ve used the Command Line Tools from Xcode 4.4 to test the procedure described bellow.

In this tutorial, I will show you how to compile from source and install the current stable version of gcc with Graphite loop optimizations on your Mac OSX Lion computer. The instructions from this tutorial were tested with Xcode 4.3.3 and Mac OSX Lion 10.7.4.

Having the latest stable 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, multithread programming or initializer lists (not supported by Clang for e.g.). It could also be useful if you want to be able to play with the latest Fortran 2008 standard.

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 and the Command Line Tools (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.

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.5.tar.bz2, mpc-0.9.tar.gz and mpfr-3.1.1.tar.gz, also save them in your Downloads folder.

For enabling Graphite loop optimizations you will two extra libraries, go to ftp://gcc.gnu.org/pub/gcc/infrastructure/ and download cloog-ppl-0.15.11.tar.gz and ppl-0.11.tar.gz.

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.5.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 mkdir build && cd build 

And now the fun part … write in your Terminal:

 1 ../configure --prefix=/usr/gcc-4.7.1 --enable-cxx 

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 the /usr/gcc-4.7.1 folder (you will need the admin password for this):

 1 sudo make install 

We will do the same steps for MPFR now, double click on mpfr-3.1.1.tar.gz to extract the archive and navigate in the extracted library:

 1 2 3 4 cd .. cd .. cd mpfr* mkdir build && cd build 

Configuration phase:

 1 ../configure --prefix=/usr/gcc-4.7.1 --with-gmp=/usr/gcc-4.7.1 

The second parameter will just inform the configure app that gmp is already installed in /usr/gcc-4.7.1.

After the configure phase is finished, we can make and install the library:

 1 2 make sudo make install 

Now, for MPC, extract the archive and go to the mpc folder:

 1 2 3 4 5 6 7 cd .. cd .. cd mpc* mkdir build && cd build ../configure --prefix=/usr/gcc-4.7.1 --with-gmp=/usr/gcc-4.7.1 --with-mpfr=/usr/gcc-4.7.1 make sudo make install 

At this time you should have finished to build and install the necessary prerequistes for gcc.

Next step is to extract the archives for cloog-ppl-0.15.11.tar.gz and ppl-0.11.tar.gz, after that you will need to write in your Terminal:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 cd .. cd .. cd ppl* mkdir build && cd build ../configure --prefix=/usr/gcc-4.7.1 --with-gmp-prefix=/usr/gcc-4.7.1 make sudo make install cd .. cd .. cd cloog* mkdir build && cd build ../configure --prefix=/usr/gcc-4.7.1 --with-gmp=/usr/gcc-4.7.1 --with-ppl=/usr/gcc-4.7.1 make sudo make install 

We are ready to compile gcc now. Be prepared that this could take more than one hour on some machines … Double click on gcc-4.7.1.tar.gz and go in the newly extracted folder:

 1 2 3 4 5 cd .. cd .. cd gcc* mkdir build && cd build ../configure --prefix=/usr/gcc-4.7.1 --enable-checking=release --with-gmp=/usr/gcc-4.7.1 --with-mpfr=/usr/gcc-4.7.1 --with-mpc=/usr/gcc-4.7.1 --with-ppl=/usr/gcc-4.7.1 --with-cloog=/usr/gcc-4.7.1 --program-suffix=-4.7 

The above command instructs the configure app where we have installed gmp, mpfr, mpc, ppl and cloog; 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 gcc-4.7, the gcc command will invoke Apple’s built compiler gcc 4.2.

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=/usr/gcc-4.7.1 --enable-checking=release --with-gmp=/usr/gcc-4.7.1 --with-mpfr=/usr/gcc-4.7.1 --with-mpc=/usr/gcc-4.7.1 --with-ppl=/usr/gcc-4.7.1 --with-cloog=/usr/gcc-4.7.1 --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 /usr/gcc-4.7.1:

 1 sudo 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=/usr/gcc-4.7.1/bin:\$PATH 

If you want to avoid writing the above command each time you open a Terminal, save the above command in the file .bash_profile from your Home folder.

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 

Let’s check if g++-4.7.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 11 12 13 14 15 16 17 18 19 20 21 //Program to test the new C++11 lambda syntax and initalizer lists #include #include using namespace std; int main() { // Test lambda cout << [](int m, int n) { return m + n;} (2,4) << endl; // Test initializer lists and range based for loop vector V({1,2,3}); cout << "V =" << endl; for(auto e : V) { cout << e << endl; } return 0; } 

Compiling and running the above lambda example:

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

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

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: