Category Archives: C++11

Getting ready for C++11

According to Herb Sutter  as of 12 August 2011 C++0X specification was approved by ISO. C++11 is the official name of the third edition of the C++ standard. Latest publicly available C++11 draft is N3242. Also watch out for the latest draft version before ISO approval N3291.

Personally I have been pretty excited about the new language features that came to be in the C++11. This blog is my attempt to make sense of all the new C++ features. In the following weeks or months (years?), I will keep posting my interpretations (read as trial and error) of the new standard. I will be using GCC to try out the new features, as it seems to be the most complete compiler in respect to the C++11 standard and also available for everyone. Other compilers that I might try out are Visual Studio 2010 and Clang. Wikipedia has a good list of compilers which have C++11 support to some degree. Here is a list of C++11 features supported by each GCC version.

I guess the first step in to start exploring the glory of the new C++ language, is to install a compatible compiler. So let’s start with that.

Installing the latest GCC release

Find a suitable mirror to get the latest (/whatever version required) GCC tarball. Basically there are two options available: either get a release or snapshot a source code. Release builds are, of course, typically more stable than the latest snapshots. In case you want to try out the latest and the greatest language features, a snapshot might be the only option. Until the official GCC 4.7 release is out, I will use the latest release currently available that is GCC 4.6.1.

Here is how I installed GCC 4.6.1 to my Linux distro, Kubuntu 11.04. The GCC toolchain that I got from package manager is GCC 4.5.2. This GCC version will be used to compile the new GCC source. For more details check the GCC official installation instructions.

  1. Download the compiler source code. Again, find suitable mirror for your location:
    wget ftp://ftp.fu-berlin.de/unix/languages/gcc/releases/gcc-4.6.1/gcc-4.6.1.tar.bz2
  2. Extract tarball e.g. like this:

    tar xvf gcc-4.6.1.tar.bz2
  3. Build configuration is probably the most important step to get right. For my purposes, I installed different GCC versions under folder /opt/compilers/gcc. This way you can avoid mangling your Linux installation. Additionally, I prefer to use the program-prefix option to avoid any confusion which GCC version is really used. This option will add user specified prefix to all GCC executables. In my setup the configure line I came up with was this:

    ./configure --prefix=/opt/compilers/gcc/gcc-4.6.1 --program-prefix=gcc-4.6.1-

    Unless you have compiled GCC in your distro before, you will most likely need to install some prerequisites. When you run the build configuration, you might get some error messages indicating which libraries or header files you are missing. I was missing the suitable GMP, MPC and MPFR libraries and header files. It’s possible to compile these libraries as part of the GCC build. The easy way is to download their source tarballs and extract them to the GCC source folder. After that just rename the extracted folders so that only the name part remains. e.g. gmp-5.0.2 would be renamed as gmp. I did exactly this with the GMP library:

    wget ftp://ftp.gmplib.org/pub/gmp-5.0.2/gmp-5.0.2.tar.bz2 
          
    tar xvf gmp-5.0.2.tar.bz2
    mv gmp-5.0.2 gmp

    Kubuntu 11.04 default installation already had libraries for libmpc and libmpfr installed. So I just installed the header files. In addition, gcc-multilib was installed to fix asm/errno.h missing error (Ubuntu bug #778047).

    sudo apt-get install libmpc-dev libmpfr-dev gcc-multilib

    Execute ./configure … script again to make sure everything is OK.

  4. Building and installing should run smoothly assuming the configuration was successfull. Just execute "make" to compile and run "sudo make install" to install built GCC binaries. The compiling part might take a while… If you run into any errors, google is your friend. There is a change that for example some header files are still missing.

That’s it! Let’s give it a try.

ubuntu@kubuntu-VirtualBox:~$ /opt/compilers/gcc/gcc-4.6.1/bin/gcc-4.6.1-gcc --version
gcc-4.6.1-gcc (GCC) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Seems to work! My next post will be about compiling a C++11 program. Meanwhile, feel free to give comments and point to any errors.

Advertisements