How to install g++ 4.7.2 & c++11 on CentOS 5.x?

I'm trying to install the latest stable g++ and c++11. My host did it through yum install and thought that it was the latest, but it's not, and they are now saying that this is beyond their level of support.

I've looked all over on how to do this, and I can't make anything work, or I have no idea what the suggested solution is saying.

What's the easiest way to install g++ 4.7.2 and c++11 on CentOS 5.9?

-
Why do you need the "latest g++"? –  vonbrand Feb 3 '13 at 22:52
I don't see anything in that that (in my very inexperienced eyes) requires some new C++ feature. To install extra software (from source, no less) isn't to be taken lightly (if you are using CentOS, it is presumably a enterprisey application). Not for somebody with hours of Linux experience, in any case. Reconsider your options carefully. –  vonbrand Feb 3 '13 at 22:56
what does require lambda so insistently? Perhaps your best bet is to install latest Fedora, where gcc-4.7.2 is the default compiler, maintained by the distribution? –  vonbrand Feb 3 '13 at 23:03
I'm no C++ expert, but "sort by N-th int in a vector of int" requires a comparison between vectors, and that is not an exotic requirement only to be fullfilled with latest C++. Sure, it might be easier, but if something that simple couldn't be done from the very first versions of STL, I'd be completely flabbergasted. Get a C++ expert on board? –  vonbrand Feb 3 '13 at 23:12
Have you considered the BOOST libraries? A large percentage of C++11 came from there... –  Aaron D. Marasco Feb 4 '13 at 0:39

From this answer to "Install gcc 4.7 on CentOS [6.x]", the easiest way to get g++ 4.7, and the required tools and libraries, for CentOS 5.x is via the devtools package:

cd /etc/yum.repos.d
wget http://people.centos.org/tru/devtools/devtools.repo
yum --enablerepo=testing-devtools-6 install devtoolset-1.0


Since you're running g++ manually (as opposed to through make), you'll need to update your $PATH variable so your shell will use the new gcc, g++, etc. binaries: export PATH=/opt/centos/devtoolset-1.0/root/usr/bin/:$PATH


At this point, your g++ should be version 4.7.0

$g++ --version g++ (GCC) 4.7.0 20120507 (Red Hat 4.7.0-5) Copyright (C) 2012 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.  Be aware that installing gcc and associated tools and libraries in this manner relies on the repository maintainer to keep their packages up to date. If you're interested in keeping your gcc up to date, you may want to consider building gcc from source. Also, compiling programs with a different version of g++ and libstdc++ than are installed on your system can cause all kinds of Fun, coping with which is beyond the scope of this answer. It may be worth moving to a Linux distribution that has support for what you're doing. - I've updated the instructions -- the package you want yum to install is devtoolset-1.0, as that contains other required development tools; and you'll want to update your $PATH variable since you're launching g++ from the command line. –  Reed Kraft-Murphy Feb 4 '13 at 0:04

Package managers are great, but (understandably) won't generally include the latest version of software. CentOS being a distribution particularly focusing on servers, favors stability (and extensive testing) over having the latest and shiniest. Here are your solutions:

Change distro

Other distributions like Arch Linux, Sabayon Linux or Debian "Sid" include newest versions a lot faster. You may consider changing distros.

Build it yourself

If you're stuck with CentOS, your best bet is to build gcc from source. It means getting the source code, making sure the dependencies are already present on your system (if not, I'm sure you can get them from the CentOS package manager), compiling the source into a binary executable, and installing the executable on your system.

It's not the easiest way to do it for sure, but you'll learn a lot about Unix in the process. Make sure to read the document a few times to make sure you get everything, and ask questions if something specific is unclear.

Be patient

At some point, this version (or newer) of gcc/g++ is going to enter the package manager. If you wait (up to several months, I'm not very familiar with the CentOS release cycle), you'll ultimately get what you want.

-