How to install custom gcc version on CentOS/RHEL

I just came back from cppcon 2017, and there were so much information about c++17 or c++2x. I just can’t wait to try out thoes features. I found lots of new versions of gcc/g++ are not ready in package form on most of OS, including CentOS/RHEL. The latest packaged gcc I can find is the one comes with Devtoolset-6, which only supports up to c++14. Here are the steps to install any custom versions of gcc/g++ on CentOS/RHEL.

  1. Install dependencies in order to build gcc

    1
    $ sudo yum -y install libmpc-devel mpfr-devel gmp-devel gcc-g++ zlib-devel
  2. For example, I want to install the latest stable version of gcc, which is gcc 7.2.0. You can find all versions here and modify the url to download the version you want.

    1
    $ curl ftp://ftp.gnu.org/pub/gnu/gcc/gcc-7.2.0/gcc-7.2.0.tar.gz -O
  3. Uncompress it

    1
    $ tar xvf gcc-7.2.0.tar.gz
  4. Configure it

    1
    2
    $ cd gcc-7.2.0
    $ ./configure --with-system-zlib --disable-multilib --enable-languages=c,c++
  5. Build it and install it

    1
    2
    $ make -j 8
    $ sudo make install
  6. By default it will install all stuff to /usr/local/. And you are done!

  7. Type /usr/local/bin/gcc -v should show you the correct version.

If you just want to try out new few features in a very light-weight form, wandbox is the best tool you can use without installing anything!