Build and Install the Last GCC on RHEL/CentOS 7

Featured image

The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Ada, and Go, as well as libraries for these languages (libstdc++,...). GCC was originally written as the compiler for the GNU operating system. The GNU system was developed to be 100% free software, free in the sense that it respects the user's freedom.

Reference: GCC official website

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 systems.

Downloading GCC source code

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

cd ~/Downloads
wget https://ftp.gnu.org/gnu/gcc/gcc-9.1.0/gcc-9.1.0.tar.gz

Build and install

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

Reference: https://stackoverflow.com/questions/39854114/set-gcc-version-for-make-in-shell

tar xzf gcc-9.1.0.tar.gz
cd gcc-9.1.0
./contrib/download_prerequisites
cd ..
mkdir gcc-9.1.0-build
cd gcc-9.1.0-build
../gcc-9.1.0/configure --enable-languages=c,c++ --disable-multilib
make
sudo make install

(If you prefer to have GCC in your HOME directory add --prefix=$HOME/gcc-9.1.0 to the configure command)

The last command will end up with message specifying where GCC has been installed.

----------------------------------------------------------------------
Libraries have been installed in:
   /usr/local/lib/../lib64

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

The compilation 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 when you configure. Post-installation

You should add the install dir of GCC to your PATH and LD_LIBRARY_PATH in order to use the newer GCC. Maybe a restart of your current session is also needed.

To do that edit your shell’s rcfile (~/.bashrc) and add :

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

Check your GCC installation

Check GCC location

whereis gcc

Check GCC compiler version

gcc --version

Test GCC

Edit a script Hello.cpp

#include <iostream>
using namespace std;

int main()
{
    	cout << "Hello World!"  << endl;
	return 0;
}

Compile & run it

g++ --std=c++14 Hello.cpp -o run
./run 

Should return

Hello World!

See also

Monitor SSIS job and package executions

date_range 02/09/2020

Featured image

How to monitor SSIS job and package executions.

Enable network connectivity between Docker containers on CentOS 8

date_range 15/08/2020

Featured image

Enable a network connectivity between Docker containers on CentOS 8.

Setup a GitHub repository to serve your Sphinx documentation

date_range 07/04/2020

Featured image

Sphinx and GitHub provide an efficient and free way to publish your documentation online. Here we describe how to do so.