Skip to content

Instantly share code, notes, and snippets.

@nchaigne
Last active September 30, 2024 01:38
Show Gist options
  • Save nchaigne/ad06bc867f911a3c0d32939f1e930a11 to your computer and use it in GitHub Desktop.
Save nchaigne/ad06bc867f911a3c0d32939f1e930a11 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.
@shamiks
Copy link

shamiks commented Nov 22, 2023

This helped a lot.

@vahid110
Copy link

Great guide. To enable gcc 9 as default, may have to run the following commands:

sudo yum install devtoolset-9-toolchain
scl enable devtoolset-9 bash

I have a question plz. If we can install the entire devtoolset-9-* which includes gcc 9 too, will it still be necessary to build gcc from source?

@nchaigne
Copy link
Author

Great guide. To enable gcc 9 as default, may have to run the following commands:

sudo yum install devtoolset-9-toolchain
scl enable devtoolset-9 bash

I have a question plz. If we can install the entire devtoolset-9-* which includes gcc 9 too, will it still be necessary to build gcc from source?

If you install devtoolset with a recent version of gcc already built, then of course there is no need to build it yourself. :)
Unless you really need a version more recent that what is provided with the devtoolset.

@vahid110
Copy link

Great guide. To enable gcc 9 as default, may have to run the following commands:

sudo yum install devtoolset-9-toolchain
scl enable devtoolset-9 bash

I have a question plz. If we can install the entire devtoolset-9-* which includes gcc 9 too, will it still be necessary to build gcc from source?

If you install devtoolset with a recent version of gcc already built, then of course there is no need to build it yourself. :) Unless you really need a version more recent that what is provided with the devtoolset.

That is correct. I did build gcc-11 from source as per instructions. But in the end I used devtoolset-11-* and things seems to work ...
Thanks

@lne-repo1
Copy link

lne-repo1 commented Jan 3, 2024

Hi
I got those errors:

make -j $(nproc) > install.log
In file included from ../../../gcc-9.2.0/libcpp/system.h:32:0,
                 from ../../../gcc-9.2.0/libcpp/directives.c:22:
/usr/lib/gcc/x86_64-linux-gnu/4.8.5/include/stdint.h:9:26: fatal error: stdint.h: No such file or directory
 # include_next <stdint.h>
                          ^
compilation terminated.
In file included from ../../gcc-9.2.0/libcpp/system.h:32:0,
                 from ../../gcc-9.2.0/libcpp/charset.c:21:
/usr/lib/gcc/x86_64-linux-gnu/4.8.5/include/stdint.h:9:26: fatal error: stdint.h: No such file or directory
 # include_next <stdint.h>
                          ^
compilation terminated.
In file included from ../../../gcc-9.2.0/libcpp/system.h:32:0,
                 from ../../../gcc-9.2.0/libcpp/charset.c:21:
/usr/lib/gcc/x86_64-linux-gnu/4.8.5/include/stdint.h:9:26: fatal error: stdint.h: No such file or directory
 # include_next <stdint.h>
                         ^

and the errors list is very long. I'm on centos 7

uname  -a
Linux myserver 3.10.0-1062.9.1.el7.x86_64 #1 SMP

do you have any ideas about this?

@IamXFL
Copy link

IamXFL commented Mar 13, 2024

QQQ~

@zhangm365
Copy link

thanks, great guides

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment