Red Hat Enterprise Linux (RHEL) distribution ships with a somewhat outdated version of the GCC compiler (4.8.3 on RHEL 7.1), which may not be suitable to your compilation requirements. For example, C11 - which supersedes C99 - is fully supported only starting from GCC 4.9).
Additionally, recent versions of GCC (GCC6, GCC7) come with improvements which help detect issues at build time and offer suggestions on how to fix them. Sometimes, these are even actually helpful!
This note describes how to build the latest GCC (7.3 as of January 2018) from sources on RHEL 7. This should be applicable as is on CentOS 7. For other Linux distributions, adapt as needed.
While this is not overly complicated, building GCC takes quite some time. So you might want to plan to do something else while it builds... a coffee break just won't make it.
Prerequisites are described here: https://gcc.gnu.org/install/prerequisites.html
- C++ compiler
yum install gcc gcc-c++
- GNU Multiple Precision Library (GMP) version 4.3.2 (or later)
Note: gmp 6.0.0 should already be installed, but you need the headers.
yum install gmp gmp-devel
- MPFR Library version 2.4.2 (or later)
yum install mpfr mpfr-devel
- MPC Library version 0.8.1 (or later)
My repository didn't have libmcp-devel available. You can get a CentOS 7 package (which will work just fine for RHEL 7), and install it manually. For example:
cd /home/build
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libmpc-devel-1.0.1-3.el7.x86_64.rpm
yum install libmpc-devel-1.0.1-3.el7.x86_64.rpm
Or you can build the latest version from sources:
cd /home/build
MPC_VERSION=1.1.0
wget https://ftp.gnu.org/gnu/mpc/mpc-${MPC_VERSION}.tar.gz
cd mpc-${MPC_VERSION}
configure
make
make install
cd /home/build
GCC_VERSION=7.3.0
wget https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.gz
tar xzvf gcc-${GCC_VERSION}.tar.gz
mkdir obj.gcc-${GCC_VERSION}
cd obj.gcc-${GCC_VERSION}
../gcc-${GCC_VERSION}/configure --disable-multilib
make
make install
Notes:
- If you have several processors available, you can benefit from a parallel build. For example,
make -j 6
will use 6 CPUs. (You might want to save a few for yourself, so you can do things on your server while gcc builds.) - Make sure you have enough space in
/home/build
(or whatever location you choose). You will need ~700 MB for gcc sources, ~5.2 GB for the build). Be prepared. - This will install gcc in
/usr/local/bin/gcc
(default prefix is/usr/local
). Your distro gcc (/usr/bin/gcc
) will not be overwritten, but if later on you need to invoke it, you will have to do so explicitly. Configure with option--prefix
if you want to change this. - Option
--disable-multilib
prevents building multiple target libraries (I don't need them, and it is simpler). - See GCC documentation for the full list of configure options.
Thanks for this manual!
MPC step, lack of decompression.
MPC_VERSION=1.1.0
wget https://ftp.gnu.org/gnu/mpc/mpc-${MPC_VERSION}.tar.gz
tar zxvf mpc-${MPC_VERSION}.tar.gz
cd mpc-${MPC_VERSION}
configure
make
make install