2011-04-20

Compiling GCC 4.6 in Ubuntu 10.10 for C++11 Development

GCC 4.6 was released recently and it has a lot of new features. It is especially nice to see all the improvements in the C++11 (C++0x) area. This short guide will show you how you can download the source code and compile GCC 4.6 on your Ubuntu 10.10 installation. If you have another distribution you can probably still use some of the information in this guide.

1. Installing Perquisites

The following packages are required to be able to compile GCC 4.6.

  • libgmp3c2 libgmp3-dev - Library for arbitrary precision arithmetic
  • libmpfr-dev - Multiple precision floating-point computation developers tools
  • libmpc-dev - Multiple precision complex floating-point library development package
  • flex - A fast lexical analyzer generator
  • bison - A parser generator that is compatible with YACC
To install them just open a terminal window and type:

$ sudo apt-get install libgmp3-dev libgmp3c2 libmpfr-dev libmpc-dev flex bison

You can also use Synaptic or  Ubuntu Software Center.

2. Obtaining The Source Code

Go to the GCC mirrors page: http://gcc.gnu.org/mirrors.html and then select the appropriate mirror closest to you. Enter the releases/gcc-4.6.0 directory and download the gcc-4.6.0.tar.bz2 file.

Example:
$ wget ftp://ftp.fu-berlin.de/unix/languages/gcc/releases/gcc-4.6.0/gcc-4.6.0.tar.bz2

Unpack the source code and go in to that directory with a terminal.  I have chosen to use /opt/gcc-4.6.0

3. Compiling The Source Code

The first thing you have to do is to configure the installation.

$ ./configure --prefix=/opt/gcc-4.6.0 --disable-bootstrap CFLAGS="-g3 -O0" --enable-languages=c,c++ --disable-multilib --program-suffix=-4.6

Run make and use the -j argument to make the compilation make better use of all your cpu:s, cores and hyperthreads.

$ make -j6

Now it is time to run make install to install it to the directory that you specified as prefix during the configure step.

$ make install


Now everything should be ready to test.

4. Testing GCC 4.6 With A C++11 Program

Enter the following source code in to a file called threading.cc. It is a simple test of C++11's threading and lambda functionality. It is a good test to see if everything works since it contains both the new language features and also makes use of the latest libraries.

#include <thread>
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{

    auto th = new thread([] { cout << "Hello, World!\n"; });
    th->join();

    delete th;

    return 0;
}

Compile with the following command:

$ ./c++-4.6 --std=c++0x -g -Wl,-rpath -Wl,/opt/gcc-4.6.0/lib64 -L/opt/gcc-4.6.0/lib64 -lpthread threading.cc -o threading

Basically it is telling the compiler to use c++0x as standard and also that it should use the standard library that you just compiled together with gcc. If you want the commands to be available in any directory you will have to put the bin directory in to the PATH variable. It is however important to know that you have to make sure that you use the compiled libraries instead of the ones that are installed with your distribution. There might be a better way to handle the libraries but I tried to do this without messing up my current gcc installation.

4 comments:

  1. You missed a prerequisite. If you don't have the texinfo package installed - or at least something that means the makeinfo program is available - then you get a really really obscure error quite a way into running the build process. [I can't remember what the error was now, but it didn't have anything to do with what the actual cause was...]

    ReplyDelete
  2. Most likely the dependencies will be (more or less) the same as for gcc-4.5, so you could just do "sudo apt-get build-dep gcc-4.5"

    ReplyDelete
  3. I created a clean virtualbox with ubuntu 10.10 and verified the steps so it should work. I already had some package installed so i needed to do it from a clean install to make sure that it worked.

    ReplyDelete
  4. You could also need to install gcc-multilib for this to work...

    sudo apt-get install gcc-multilib

    ReplyDelete