Compile & Upgrade GCC on CentOS

1. About

GCC refers to the GNU Compiler Suite, which refers to a set of programming language compilers, free software released under the GPL and LGPL licenses, and is also a key part of the GNU project and one of the main components of the GNU tool chain.
GCC is also often considered the de facto standard for cross-platform compilers.
Started development by Richard Matthew Stallman in 1985 and is now maintained by the Free Software Foundation.
Formerly known as the GNU C Language Compiler, because it originally could only handle the C language.
glibc is the libc library released by GNU, that is, the c runtime library.
glibc is the lowest-level api in the linux system, and almost any other runtime library will depend on glibc.
In addition to encapsulating the system services provided by the linux operating system, glibc itself also provides the realization of many other necessary functional services.
Since glibc includes almost all UNIX common standards, it can be imagined that its content is all-encompassing.

However, as we all know, backwardness is one of the necessary and insufficient conditions for stability, the latest version of the software will go through layers of testing and review before entering the system image, so most of the gcc/glibc that comes with the system are backward and stable (~ ~Although it is much higher than Baidu's internal version~~).

When running some programs compiled with a higher version of gcc/glibc, if you use a lower version of gcc/glibc, there will be a lack of dependencies and runtime libraries. Therefore, it is recommended to update the system as soon as possible after installing the system, so that the migration cost is low.

Warning:
If you are in a production environment, be sure to take backup measures such as snapshots before upgrading, otherwise it will be difficult to remedy problems in the first place.

Compiling GCC and its supporting software is very troublesome, so once it is compiled on one machine, it can be packaged into tar.gz and decompressed to the /usr/local/ directory of other machines, and then modify the software according to the last tutorial Link.

2. Confirm the version

Generally speaking, if there is no business on the system that requires a lower version of gcc/glibc (such as Baidu?), just install the latest version directly, which will avoid many pitfalls. For example, if you find that the upgrade is complete, the version is still not high enough .

exist

GCC official website
https://gcc.gnu.org/

You can see the latest version of GCC (eg: 12.2.0)

Find a suitable software source image (Take Tencent Cloud as an example)

Reference article:

Comparison of domestic software sources
https://blog.tsinbei.com/archives/238/
Server Optimization (2) Replacing Software Sources
https://blog.tsinbei.com/archives/237/

Download links can be found:

https://mirrors.cloud.tencent.com/gnu/gcc/gcc-12.2.0/gcc-12.2.0.tar.gz

Tip:
Please follow the method above and replace the download link below with the link you found.

3, compile and install

Before compiling GCC, you need to compile a higher version of GMP/MPFR/MPC.
Take gmp-6.2.1, mpfr-4.1.0, mpc-1.2.1 as examples.

The following three orders cannot be exchanged because the order of dependencies is different.

3.1, compile GMP

implement:

Text
1
2
3
4
5
6
7
cd ~
wget https://gmplib.org/download/gmp/gmp-6.2.1.tar.xz
xz -d gmp-6.2.1.tar.xz
tar xvf gmp-6.2.1.tar
cd gmp-6.2.1
./configure --prefix=/usr/local/gmp-6.2.1
make -j8 && make install -j8

3.2, compile MPFR

implement:

Text
1
2
3
4
5
6
cd ~
wget https://www.mpfr.org/mpfr-current/mpfr-4.1.0.tar.gz
tar -zxvf mpfr-4.1.0.tar.gz
cd mpfr-4.1.0
./configure --prefix=/usr/local/mpfr-4.1.0 --with-gmp=/usr/local/gmp-6.2.1
make -j8 && make install

3.3, compile MPC

implement:

Text
1
2
3
4
5
6
cd ~
wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.2.1.tar.gz
tar -zxvf mpc-1.2.1.tar.gz
cd mpc-1.2.1
./configure --prefix=/usr/local/mpc-1.2.1 --with-gmp=/usr/local/gmp-6.2.1 --with-mpfr=/usr/local/mpfr-4.1.0
make -j8 && make install -j8

3.4, compile GCC

The file generated by GCC source code + compilation is about 8GB, please ensure that the installation directory has enough space.

If the following command is executed and the command cannot be found, you can first saturate the installation dependencies:

Bash
1
yum -y install xz wget gcc-c++ ncurses ncurses-devel cmake make perl openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype* make gcc-c++ cmake bison-devel bison perl perl-devel perl perl -devel glibc-devel.i686 glibc-devel libaio ntpdate readline-devel zlib.x86_64 zlib-devel.x86_64 libcurl-* net-tool* sysstat lrzsz dos2unix telnet.x86_64 nethogs iftop iotop unzip ftp.x86_64 xfssh-miscps* exp ect client* libaio libaio1 libnuma bzip2 epel-release

Otherwise, you can skip it and execute the following command directly.

Download and unzip:

Bash
1
2
3
4
5
6
cd ~
wget --no-check-certificate https://mirrors.cloud.tencent.com/gnu/gcc/gcc-12.2.0/gcc-12.2.0.tar.gz
tar -zxvf gcc-12.2.0.tar.gz
cd gcc-12.2.0
mkdir build
cd build

Build configuration:

Comment first then view it after your comment is approved. Join QQ Group to display all hidden texts.

Part of the command reference here: Zhihu@IceBear1

Remember to replace the GCC version number 12.2.0 and the final GMP/MPFR/MPC version number.

Start compiling:


Comment first then view it after your comment is approved. Join QQ Group to display all hidden texts.

Command reference here: IceBear1
  • Change the number in j12 to the number of your CPU cores. If it cannot be fully occupied for a long time, you can choose the next best thing, for example: use j4 for 8 cores.
  • O3 is optimized by the compiler, if you are afraid of problems, you can change it to O2.

Finally, just wait a few hours.

If you exit with an error in the middle, you need to execute:

Bash
1
2
3
4
cd ~
cd gcc-12.2.0
make clean
rm -rf CMakeCache.txt

Check the dependencies again and recompile, otherwise the obj generated in the middle of the last compilation may be incomplete, causing it to get stuck in the middle of the recompilation.

You can also completely clean up the last compiled file:

Bash
1
2
3
4
5
cd ~
cd gcc-12.2.0
rm -rf ./build
mkdir build
cd build

Backup old glibc soft links:

Bash
1
2
3
4
5
mv /usr/bin/gcc /usr/bin/gcc.old
mv /usr/bin/g++ /usr/bin/g++.old
mv /usr/bin/c++ /usr/bin/c++.old
mv /usr/bin/cpp /usr/bin/cpp.old
mv /usr/bin/gcov /usr/bin/gcov.old

Link all new programs as soft links:

Bash
1
ln -sf /usr/local/gcc-12.2.0/bin/* /usr/bin/

Update the glibc dynamic library:

Bash
1
2
3
4
5
rm -rf /usr/local/gcc-12.2.0/lib64/*.py
echo -e "/usr/local/gcc-12.2.0/lib64" >> /etc/ld.so.conf
ldconfig
cd /lib64
ln -sf libstdc++.so.6.0.30 libstdc++.so.6

Set the header file:

Bash
1
ln -sv /usr/local/gcc-12.2.0/include/c++/12.2.0 /usr/include/c++/12.2.0

an examination:

Bash
1
2
3
4
gcc --version
g++ --version
c++ --version
strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX

You're done!

Compile & Upgrade GCC on CentOS

https://blog.tsinbei.com/en/archives/652/

Author
Hsukqi Lee
Posted on

2022-12-08

Edited on

2022-12-08

Licensed under

CC BY-NC-ND 4.0

Comments

Name
Mail
Site
None yet