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.
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.
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:
A number of development tools are referenced by the meta-package named build-essential, so I started by making sure this package is installed:
This gave me the expected GCC v4.6 C++ compiler:
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:
Now take a look to see if we can find the GCC packages:
Install the GCC v4.7 C/C++ compilers with the following command:
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:
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:
From this point forward, the only thing required when switching compilers is this (relatively) simple command:
Calling the C++ compiler with --version confirms the right version is correctly installed:
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: