../

Summary

I had a bit of extra spare time over the 2011 holiday season, and I wanted to test out some of the new C++11 features. But I know the old GCC C++ compiler I have installed in my usual LTS Ubuntu 10.04 development environment is out-of-date, so the first thing I needed to do is upgrade my OS and GCC to something decent. Here is how I accomplished this task.

Ubuntu 12.04 (beta)

First thing was to change my operating system. From Canonical, I downloaded the latest daily live .iso file. Here is a decent place to start looking: http://cdimage.ubuntu.com/daily-live/current/precise-desktop-amd64.iso. These live-cd images are a breeze to install using VirtualBox, and within 30 minutes I had a decent development environment installed.

Installing GCC v4.6

When Ubuntu 12.04 is first installed, it defaults to GCC v4.6. And just as important, it only includes the 'C' compiler, not the 'C++' compiler:

$ which g++ gcc /usr/bin/gcc $ gcc --version gcc (Ubuntu/Linaro 4.6.2-9ubuntu1) 4.6.2

A number of development tools are referenced by the meta-package named build-essential, so I started by making sure this package is installed:

$ sudo apt-get install build-essential

This gave me the expected GCC v4.6 C++ compiler:

$ which g++ gcc /usr/bin/g++ /usr/bin/gcc $ g++ --version g++ (Ubuntu/Linaro 4.6.2-9ubuntu1) 4.6.2

Installing GCC v4.7

A quick search on Google turned up a PPA with the newer GCC v4.7, which has support for nearly all the new C++11 features. Add this PPA with the following commands:

$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test $ sudo apt-get update

Now take a look to see if we can find the GCC packages:

$ sudo apt-cache search "g\+\+" ... g++ - GNU C++ compiler g++-multilib - GNU C++ compiler (multilib files) g++-4.4 - GNU C++ compiler g++-4.4-multilib - GNU C++ compiler (multilib files) g++-4.5 - The GNU C++ compiler g++-4.5-multilib - The GNU C++ compiler (multilib files) g++-4.6 - GNU C++ compiler g++-4.6-multilib - GNU C++ compiler (multilib files) g++-4.7 - GNU C++ compiler g++-4.7-multilib - GNU C++ compiler (multilib files) ...
More information on the multilib packages can be found here, though most people shouldn't need it.

Install the GCC v4.7 C/C++ compilers with the following command:

$ sudo apt-get install gcc-4.7 g++-4.7

Switching compilers

Installing the v4.7 compilers does not remove the previous v4.6 compiler. This is very important, if you skipped the previous sentence, now is a good time to go back and re-read it! The system now has 2 'C' compilers and 2 'C++' compilers:

$ which g++ /usr/bin/g++ $ ls -lh /usr/bin/g++ lrwxrwxrwx 1 root root 7 Dec 12 17:04 /usr/bin/g++ -> g++-4.6 $ ls -lh /usr/bin/g++* lrwxrwxrwx 1 root root 7 Dec 12 17:04 /usr/bin/g++ -> g++-4.6 -rwxr-xr-x 1 root root 349K Dec 17 04:17 /usr/bin/g++-4.6 -rwxr-xr-x 1 root root 566K Dec 22 02:37 /usr/bin/g++-4.7

Luckily, there is a very simple and standard method for switching between applications that provide identical (or near-identical) functionality: update-alternatives. If you've never had to use this before, go ahead and run man update-alternatives.

We need to let update-alternatives know we have 2 C/C++ compilers, create a record for each one, and then configure which one we want to use. This is done with the following:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7 sudo update-alternatives --config gcc
Note the use of "slave". This ensures when we change the configuration for gcc, that we automatically update the configuration for g++. This is important so the compilers aren't out-of-sync.

From this point forward, the only thing required when switching compilers is this (relatively) simple command:

sudo update-alternatives --config gcc

Calling the C++ compiler with --version confirms the right version is correctly installed:

$ g++ --version g++ (Ubuntu/Linaro 4.7-20111222-0ubuntu1) 4.7.0 20111222 (experimental) [trunk revision 182617]

Testing compilers

The only thing left is to test if the new v4.7 compiler is working as expected. There are several convenient pages on the GNU GCC web site which lists which features of C++11 are supported in each version of the compiler:

Comparing the v4.6 and v4.7 page, one easy-to-test feature where support was first added in v4.7 is delegating constructors. So I wrote up a quick test, which I used to confirm the difference between the two compilers:

$ sudo update-alternatives --config gcc # select the old v4.6 compiler $ g++ -std=c++0x 2011-12-24_GCCv47.cpp 2011-12-24_GCCv47.cpp: In constructor ‘A::A()’: 2011-12-24_GCCv47.cpp:36:2: error: type ‘A’ is not a direct base of ‘A’ $ sudo update-alternatives --config gcc # select the new v4.7 compiler $ g++ -std=c++11 2011-12-24_GCCv47.cpp $ ./a.out a.value == 3
Also note how the -std=c++0x parameter from older GCC g++ compilers was renamed to -std=c++11 in v4.7.
Last modified: 2011-12-31
Stéphane Charette, stephanecharette@gmail.com
../