Installing GCC from Source

My laptop is running CentOS 7 with a version of GCC installed via yum, but this version is too old, so I need to install a newer version of GCC.

Installing GCC from Source

1. First, download from the internethttp://mirror.hust.edu.cn/gnu/gcc/

Find the appropriate version and download it.

Installing GCC from Source

2. Extract the compressed package

# cd /usr/local/# tar -zxvf gcc-6.4.0.tar.gz

3. Download the required dependencies for compilation

1) If Linux has a network connection

# cd gcc-6.4.0# ./contrib/download_prerequisites 

2) If there is no network connection

Resolve the dependency issues yourself; fix any errors that arise.

4. Compile GCC

# yum install -y gcc-c++ glibc-static gcc // It is recommended to install this package to avoid errors# ./configure --prefix=/usr/local/gcc --enable-bootstrap --enable-checking=release --enable-languages=c,c++ --disable-multilib

–enable-bootstrap: This option allows the program generated by the first compilation to be used for the second compilation, and then the program generated again is used for the third compilation, checking the correctness of the second and third results, which is a redundant compilation check. In a non-cross-compilation environment, this value is set to enable by default and does not need to be specified explicitly; in a cross-compilation environment, it needs to be explicitly set to disable.

–enable-checking=release: This checks the consistency of the code generated during compilation against the standards of a software release version. Setting this option to enable does not change the binary results generated by the compiler but will increase compilation time. This option is only supported by the GCC compiler. Generally, for this option, if the hardware configuration of the machine is low and you do not want to wait too long for compilation time, you can set it to disable; however, this increases the risk of unexpected errors, so it should be used with caution. You can also set –disable-bootstrap and –disable-checking together, which can significantly speed up the compilation process.

–enable-threads=posix: This enables POSIX standard thread support. To ensure that the program runs correctly on a Linux distribution that complies with POSIX standards, this option should be enabled. The available values depend on the type of host or target operating system, including aix, dec, solaris, win32, etc. If you are using another UNIX-like system, you need to set the corresponding value.

–enable-languages=c,c++: This specifies the supported high-level language types and runtime libraries. All languages that can be set include ada, c, c++, Fortran, java, objc, obj-c++, GO, etc. Here, only c and c++ are enabled because the more languages supported, the more corresponding static and dynamic libraries need to be installed, along with various dependencies, making management more complicated and the size larger.

–disable-multilib: If your operating system is 32-bit, this is set to disable by default, meaning GCC can only generate 32-bit executable programs. If your operating system is 64-bit, this is set to enable by default, meaning that when compiling other source files with GCC, you can use the -m32 option to decide whether to generate 32-bit machine code. If you want to prevent generating 32-bit code on a 64-bit system, set –disable-multilib.

–enable-gather-detailed-mem-stats: This allows for the collection of detailed memory usage information. If this parameter is set to enable, the compiled GCC executable can output real-time memory usage when compiling other programs using the -fmem-report option.

–with-long-double-128: This specifies the long double type as 128 bits (16 bytes!). If set to without, the long double type will be 64 bits (8 bytes), which will be the same as the ordinary double type. By default, this is already set to 128 bits when compiled with Glib version 2.4 or higher.

5. Install GCC

# make && make install

6. Export environment variables

# vim /etc/profile.d/gcc.sh
export PATH=/usr/local/gcc/bin:$PATH
# source /etc/profile.d/gcc.sh
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc/libexec/gcc/x86_64-pc-linux-gnu/6.4.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --prefix=/usr/local/gcc --enable-bootstrap --enable-checking=release --enable-languages=c,c++ --disable-multilib
Thread model: posix
gcc version 6.4.0 (GCC)

7. Export header files

# ln -sv /usr/local/gcc/include/ /usr/include/gcc

8. Export library files

# vim /etc/ld.so.conf.d/gcc.conf
/usr/local/gcc/lib64
# ldconfig -v
# ldconfig -p | grep gcc  // Verify if exported

Installing GCC from Source

This article is a summary of my experiences during self-study. If you have any suggestions or comments, please leave a message at the end of the article.Thank you.

Leave a Comment