The good thing about Centos is it's free and RHEL-like. The bad thing is it is always behind RHEL so if you want to play with the latest greatest packages you have to hunt them down, unless you want to compile from source, which gets tedious rather quickly.

Whilst still classified as "experimental", support for the latest ISO standard for C++, C++11, has been progressively added to gcc. This page details the GCC versions that first supported each given C++11 feature. From this we can tell that GCC v4.8 is what we're after. Regrettably, the most recent package with Centos 6.5 is gcc 4.4.7, which is very old!

[damien@web1 ~]$ cat /etc/centos-release
CentOS release 6.5 (Final)

[damien@web1 ~]$ uname -a
Linux web1 2.6.32-431.20.5.el6.x86_64 #1 SMP Fri Jul 25 08:34:44 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

[damien@web1 ~]$ gcc --version
gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4)
Copyright (C) 2010 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.

So after some searching for relevant EL6 packages I found this repository (http://puias.princeton.edu/data/puias/DevToolset/6/x86_64/) of EL6 x86_64 packages that includes a good set of Developer Tools including GCC 4.8.1. A big thank you to the good folks at Princeton for providing this!

To make use of yum to easily install this package and any dependencies we need to add a new repository. These live under /etc/yum.repos.d/ so, as root, we need to create a file called, say /etc/yum.repos.d/DevToolset.repo and add to it the following information:

[DevToolset-2]
name=RedHat DevToolset v2 $releasever - $basearch
baseurl=http://puias.princeton.edu/data/puias/DevToolset/$releasever/$basearch/
enabled=1
gpgcheck=0

Here "baseurl" is a URL to the directory where the repodata directory of a repository is located. Note that Yum always expands the $releasever, $arch, and $basearch variables in URLs. [Read more that that here]. I've disabled the GPG signature check simply for convenience, and have set enabled=1 to instruct Yum to include this repository as a package source.

Now we can use yum to install the packages we want...

sudo yum install devtoolset-2-gcc-4.8.1 devtoolset-2-gcc-c++-4.8.1

This places the relevant binaries in /opt/rh/devtoolset-2/root/usr/bin...

[damien@web1 ~]$ ll /opt/rh/devtoolset-2/root/usr/bin
total 5544
-rwxr-xr-x 4 root root 753392 Sep 12 2013 c++
lrwxrwxrwx 1 root root 3 Jul 7 12:11 cc -> gcc
-rwxr-xr-x 1 root root 751872 Sep 12 2013 cpp
-rwxr-xr-x 4 root root 753392 Sep 12 2013 g++
-rwxr-xr-x 2 root root 749872 Sep 12 2013 gcc
-rwxr-xr-x 1 root root 24768 Sep 12 2013 gcc-ar
-rwxr-xr-x 1 root root 24736 Sep 12 2013 gcc-nm
-rwxr-xr-x 1 root root 24736 Sep 12 2013 gcc-ranlib
-rwxr-xr-x 1 root root 311088 Sep 12 2013 gcov
-r-xr-xr-x 1 root root 348 Mar 12 13:19 sudo
-rwxr-xr-x 4 root root 753392 Sep 12 2013 x86_64-redhat-linux-c++
-rwxr-xr-x 4 root root 753392 Sep 12 2013 x86_64-redhat-linux-g++
-rwxr-xr-x 2 root root 749872 Sep 12 2013 x86_64-redhat-linux-gcc
[damien@web1 ~]$

And we can confirm we have the desired version of GCC..

[damien@web1 ~]$ /opt/rh/devtoolset-2/root/usr/bin/gcc --version
gcc (GCC) 4.8.1 20130715 (Red Hat 4.8.1-4)
Copyright (C) 2013 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.

Now it is simply a matter of making this GCC version the one that is found by default. We can do that by creating a symbolic link in the /usr/local/bin folder to the relevant GCC binary folder. Since the /usr/local/bin directory is including very early on in the $PATH variable the relevant binary should be found without giving the full path.

[damien@web1 ~]$ sudo ln -s /opt/rh/devtoolset-2/root/usr/bin/* /usr/local/bin/

[damien@web1 ~]$ hash -r

[damien@web1 ~]$ which gcc
/usr/local/bin/gcc

[damien@web1 ~]$ gcc --version
gcc (GCC) 4.8.1 20130715 (Red Hat 4.8.1-4)
Copyright (C) 2013 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.

Testing all is good with a small program with C++11 features...

#include <vector>
#include <iostream>

int main( )
{
std::cout << "Testing some c++11 things...\n" << std::endl;

std::vector myvector;
for (int i=1; i<=5; i++) myvector.push_back(i);

std::cout << "myvector contains:";

//use auto instead of the long-winded std::vector::iterator
for (auto it = myvector.begin() ; it != myvector.end(); ++it)
std::cout << ' ' << *it;

std::cout << '\n';

return 0;
}

To tell the compiler we want to use C++11 we can use the std=c++11, or std=gnu++11 compiler option...

[damien@web1 C11]$ g++ -Wall -std=c++11 ./test.cpp -o testc11
[damien@web1 C11]$ ./testc11
Testing some c++11 things...

myvector contains: 1 2 3 4 5
[damien@web1 C11]$

Bookmark and Share