Skip to main content
  1. Posts/

How to Compile and Install Latest Version of GCC on CentOS 7

··289 words·2 mins·
Linux C++ CentOS GCC
Table of Contents

The default GCC that comes with the CentOS 7.2 is GCC 4.8.5, which does not support the complete C++11 standard, for example, it does not fully support regular expressions. In order to use regular expression functions, we need to install at least GCC 4.9.0. The following installation procedure is applicable to CentOS 7 and are not tested on other Linux systems. Also you have to make sure that you have root privelege.

Update:

GCC 8.3 has been released on Feb 22, 2019. The installation process is the same as prvevious versions. Download the corrent tar file from the GNU ftp server, compile and install it.

Downloading GCC source code
#

You can download the GCC source code from the official GNU ftp. I choose to install version 5.4.0.

curl https://ftp.gnu.org/gnu/gcc/gcc-5.4.0/gcc-5.4.0.tar.bz2 -O
tar jxvf gcc-5.4.0.tar.bz2

Install dependencies
#

We need to install 3 dependencies packages. It is recommended to install these packages through yum.

yum install gmp-devel mpfr-devel libmpc-devel

Configuration and install
#

Unlike other packages, it is recommended to create another build directory outside of the GCC source directory to build GCC.

mkdir gcc-5.4.0-build
cd gcc-5.4.0-build
../gcc-5.4.0/configure --enable-languages=c,c++ --disable-multilib
make -j$(nproc) && make install

The comiplation process may take a long time and you need to be patient. It will install GCC under /usr/local. You can change the install dir using --prefix option if you prefer.

Post-installation
#

You should add the install dir of GCC to your PATH and LD_LIBRARY_PATH in order to use the newer GCC. Add the following settings to /etc/profile:

export PATH=/usr/local/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH

Maybe a restart of your current session is also needed.

You can download the whole install script here.

References
#

Related

Commonly-used Building Options Explained
··424 words·2 mins
Linux GCC C C++
Install LLVM/Clangd from Source on CentOS 7
··445 words·3 mins
Linux Clang C++
Switch Command with update-alternatives on Ubuntu
··457 words·3 mins
Linux Ubuntu GCC