Building gcc-4.7 on Ubuntu 12.04
Posted on April 13, 2012 by Sol

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
2
sudo apt-get update
sudo apt-get upgrade

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

1
2
3
4
sudo apt-get install g++
sudo apt-get install gawk
sudo apt-get install m4
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: http://gcc.gnu.org/mirrors.html 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
2
cd ~
cd Downloads

We will start by compiling the gmp library:

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

Now, for MPC:

1
2
3
4
5
6
7
cd ..
cd ..
cd mpc*
mkdir build && cd build
../configure --build=x86_64-linux-gnu --prefix=/usr/gcc_4_7 --with-gmp=/usr/gcc_4_7 --with-mpfr=/usr/gcc_4_7
make
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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
cd ..
cd ..
mkdir build && cd build
export LD_LIBRARY_PATH=/usr/gcc_4_7/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/
export C_INCLUDE_PATH=/usr/include/x86_64-linux-gnu
export CPLUS_INCLUDE_PATH=/usr/include/x86_64-linux-gnu
../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
make
sudo ln -s /usr/lib/x86_64-linux-gnu /usr/lib64
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
2
export PATH=/usr/gcc_4_7/bin:$PATH
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
2
cd ~
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
 2
 3
 4
 5
 6
 7
 8
 9
10
//Program to test the new C++11 lambda syntax
#include <iostream>

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 has also support for C++11 threads:

 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 <iostream>
#include <thread>

//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;
}

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

1
2
3
g++-4.7 -std=c++11 tst_threads.cpp -o tst_threads -lpthread
./tst_threads
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
 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.f90s):

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:

blog comments powered by Disqus