Solarian Programmer

My programming ramblings

Building gcc-4.7 on Ubuntu 12.04

Posted on April 13, 2012 by Sol

26 August 2014 update:

There is an updated version of this article for Ubuntu 14.04 and GCC 4.9.

15 October 2012 update:

I’ve updated the article for gcc-4.7.2 and his dependencies: gmp-5.0.5, mpfr-3.1.1 and mpc-1.0.1.

This is a short how to compile from sources gcc-4.7 on Ubuntu 12.04. I was surprised to find that the process of building gcc-4.7 from sources on Ubuntu is so complicated, after all, this is a Linux system …

First check that you have the default Ubuntu gcc and binutils installed, in principle the above are already installed if you use the 64 bits Desktop edition of Ubuntu. In the rest of this tutorial I will suppose you will use a Terminal to input all the described instructions.

If your system is not up to date, you can install the latest updates with:

1 sudo apt-get update
2 sudo apt-get upgrade

Now, let’s start by installing a few prerequisites:

1 sudo apt-get install g++
2 sudo apt-get install gawk
3 sudo apt-get install m4
4 sudo apt-get install gcc-multilib

We will begin by downloading the last snapshot of gcc-4.7 from the GNU website, so go to: and select the last stable release of gcc-4.7, which at the time of this writing is gcc-4.7.2.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.5.tar.bz2, mpc-1.0.1.tar.gz and mpfr-3.1.1.tar.gz, also save them in your Downloads folder.

From your Terminal navigate to the Downloads folder where you have previously saved and extracted gcc-4.7.2, gmp-5.0.5, mpfr-3.1.1 and mpc-1.0.1:

1 cd ~
2 cd Downloads

We will start by compiling the gmp library:

1 cd gmp*
2 mkdir build && cd build
3 ../configure --prefix=/usr/gcc_4_7 --build=x86_64-linux-gnu
4 make
5 sudo make install

In a few minutes you will have a compiled and installed gmp library. If you see no error message … congratulations!

We will redo the same steps for MPFR now:

1 cd ..
2 cd ..
3 cd mpfr*
4 mkdir build && cd build
5 ../configure --build=x86_64-linux-gnu --prefix=/usr/gcc_4_7 --with-gmp=/usr/gcc_4_7
6 make
7 sudo make install

Now, for MPC:

1 cd ..
2 cd ..
3 cd mpc*
4 mkdir build && cd build
5 ../configure --build=x86_64-linux-gnu --prefix=/usr/gcc_4_7 --with-gmp=/usr/gcc_4_7 --with-mpfr=/usr/gcc_4_7
6 make
7 sudo 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.

The next set of instructions will compile the C, C++ and Fortran compilers, appending -4.7 as prefix to each compiler:

 1 cd ..
 2 cd ..
 3 mkdir build && cd build
 4 export LD_LIBRARY_PATH=/usr/gcc_4_7/lib:$LD_LIBRARY_PATH
 5 export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/
 6 export C_INCLUDE_PATH=/usr/include/x86_64-linux-gnu
 7 export CPLUS_INCLUDE_PATH=/usr/include/x86_64-linux-gnu
 8 ../gcc-4.7.2/configure --build=x86_64-linux-gnu --prefix=/usr/gcc_4_7 --with-gmp=/usr/gcc_4_7 --with-mpfr=/usr/gcc_4_7 --with-mpc=/usr/gcc_4_7 --enable-checking=release --enable-languages=c,c++,fortran --disable-multilib --program-suffix=-4.7
 9 make
10 sudo ln -s /usr/lib/x86_64-linux-gnu /usr/lib64
11 sudo make install

Thanks to sjbeans for the tip of adding the export LD_LIBRARY_PATH line to the above set of instructions.

If you want to permanently add the compilers to your system’s path add this line at the end of your .bashrc file:

1 export PATH=/usr/gcc_4_7/bin:$PATH
2 export LD_LIBRARY_PATH=/usr/gcc_4_7/lib:/usr/gcc_4_7/lib64:$LD_LIBRARY_PATH

Don’t know how to find .bashrc ? No problem, you can find it from your Terminal:

1 cd ~
2 gedit .bashrc

Paste at the end .bashrc the above export line, save the file and close gedit. Now you just need to instruct Bash to reload .bashrc (this is automatically done when you restart you machine):

1 . .bashrc

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

Compiling and running the above lambda example will return ... 6:

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

g++-4.7 has also support for C++11 threads:

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

Compiling and running the above C++11 threads example (saved in a file named tst_threads.cpp ) can be done with:

1 g++-4.7 -std=c++11 tst_threads.cpp -o tst_threads -lpthread
2 ./tst_threads
3 Hello, World!

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

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

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

1 gfortran-4.7 tst_concurrent_do.f90 -o tst_concurrent_do
2 ./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:

comments powered by Disqus