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...
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... And we can confirm we have the desired version of GCC.. 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]$