Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackYoung/1f2d866d98f028df71718e6f16523f1e to your computer and use it in GitHub Desktop.
Save JackYoung/1f2d866d98f028df71718e6f16523f1e to your computer and use it in GitHub Desktop.
Building GCC 9.2.0 on CentOS 7

Building GCC 9.2.0 on CentOS 7

Introduction

CentOS 7 distribution (as well as RHEL 7) ships with a somewhat outdated version of the GCC compiler (4.8.5 on CentOS 7.5), 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, GCC8, GCC9) 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 (9.2.0 as of October 2019) from sources on CentOS 7. This should be applicable as is on RHEL 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

Prerequisites are described here: https://gcc.gnu.org/install/prerequisites.html

  • C++ compiler

yum install gcc gcc-c++

Required support libraries, listed hereafter, can be downloaded automatically using script download_prerequisites included in the GCC archive. It's convenient, so we'll do that.

Build and install gcc

cd /home/build
GCC_VERSION=9.2.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 gcc-${GCC_VERSION}
./contrib/download_prerequisites
cd ../obj.gcc-${GCC_VERSION}
../gcc-${GCC_VERSION}/configure --disable-multilib --enable-languages=c,c++
make -j $(nproc)
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 ~1 GB for gcc sources, ~6 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).
  • Option --enable-langagues allows to have a leaner and faster build if you only need (for example) C and C++.
  • See GCC documentation for the full list of configure options.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment