最小化安装CentOS 8快速编译安装GCC10.2.0最新版本

一、前言

本文介绍了个人认为最简单的 CentOS 编译安装最新版 gcc 的方法。已将安装包一并打包上传。点击 gcc-10.2.0-all.tar.gz 获得。

二、环境

2.1 系统

CentOS 8 1905 最小化(minimal)

1
2
3
4
[root@cent8-02-jeremy jeremy]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)
[root@cent8-02-jeremy jeremy]# uname -a
Linux cent8-02-jeremy 4.18.0-80.el8.x86_64 #1 SMP Tue Jun 4 09:19:46 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

2.2 yum仓库

阿里源, 详见:阿里源CentOS镜像配置

三、准备工作

3.1 安装依赖

dnf install --enablerepo PowerTools gcc gcc-c++ glibc-static libstdc++-static make tar bzip2 -y

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[root@cent8-02-jeremy jeremy]# dnf install --enablerepo PowerTools gcc gcc-c++ glibc-static libstdc++-static make tar bzip2 -y
Repository AppStream is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository PowerTools is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
CentOS-8 - AppStream 1.0 MB/s | 5.8 MB 00:05
CentOS-8 - PowerTools - mirrors.aliyun.com 710 kB/s | 1.9 MB 00:02
CentOS-8 - Base - mirrors.aliyun.com 819 kB/s | 2.2 MB 00:02
CentOS-8 - Extras - mirrors.aliyun.com 3.1 kB/s | 7.3 kB 00:02
Dependencies resolved.
=================================================================================
Package Arch Version Repository Size
=================================================================================
Installing:
gcc x86_64 8.3.1-5.el8.0.2 AppStream 23 M
gcc-c++ x86_64 8.3.1-5.el8.0.2 AppStream 12 M
glibc-static x86_64 2.28-101.el8 PowerTools 2.0 M
libstdc++-static x86_64 8.3.1-5.el8.0.2 PowerTools 598 k
bzip2 x86_64 1.0.6-26.el8 base 60 k
make x86_64 1:4.2.1-10.el8 base 498 k
tar x86_64 2:1.30-4.el8 base 838 k
Upgrading:
glibc x86_64 2.28-101.el8 base 3.7 M
glibc-common x86_64 2.28-101.el8 base 1.3 M
glibc-langpack-en x86_64 2.28-101.el8 base 821 k
libgcc x86_64 8.3.1-5.el8.0.2 base 78 k
libgomp x86_64 8.3.1-5.el8.0.2 base 203 k
libstdc++ x86_64 8.3.1-5.el8.0.2 base 451 k
Installing dependencies:
cpp x86_64 8.3.1-5.el8.0.2 AppStream 10 M
isl x86_64 0.16.1-6.el8 AppStream 841 k
libmpc x86_64 1.0.2-9.el8 AppStream 59 k
libstdc++-devel x86_64 8.3.1-5.el8.0.2 AppStream 2.0 M
libxcrypt-static x86_64 4.1.1-4.el8 PowerTools 53 k
binutils x86_64 2.30-73.el8 base 5.7 M
glibc-devel x86_64 2.28-101.el8 base 1.0 M
glibc-headers x86_64 2.28-101.el8 base 473 k
kernel-headers x86_64 4.18.0-193.14.2.el8_2 base 4.0 M
libxcrypt-devel x86_64 4.1.1-4.el8 base 25 k

Transaction Summary
=================================================================================
Install 17 Packages
Upgrade 6 Packages

Total download size: 71 M

其他依赖请看 7.1 可选依赖

3.2 下载最新包

curl -O https://mirror.tuna.tsinghua.edu.cn/gnu/gcc/gcc-10.2.0/gcc-10.2.0.tar.gz

1
2
3
4
[root@cent8-02-jeremy jeremy]# curl -O https://mirror.tuna.tsinghua.edu.cn/gnu/gcc/gcc-10.2.0/gcc-10.2.0.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 123M 100 123M 0 0 15.1M 0 0:00:08 0:00:08 --:--:-- 11.5M

3.3 解压

tar xf gcc-10.2.0.tar.gz

1
[root@cent8-02-jeremy jeremy]# tar xf gcc-10.2.0.tar.gz

3.4 安装gcc依赖库

contrib/download_prerequisites

1
2
3
4
5
6
7
8
[root@cent8-02-jeremy jeremy]# cd gcc-10.2.0
[root@cent8-02-jeremy gcc-10.2.0]# contrib/download_prerequisites

gmp-6.1.0.tar.bz2: OK
mpfr-3.1.4.tar.bz2: OK
mpc-1.0.3.tar.gz: OK
isl-0.18.tar.bz2: OK
All prerequisites downloaded successfully.

四、正式安装

4.1 建立编译目录

1
2
3
[root@cent8-02-jeremy gcc-10.2.0]# cd ..
[root@cent8-02-jeremy jeremy]# mkdir gcc-build
[root@cent8-02-jeremy jeremy]# cd gcc-build/

4.2 配置

根据系统7.2 gcc 配置,得以下配置指令:

../gcc-10.2.0/configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --disable-multilib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-initfini-array --enable-gnu-indirect-function --enable-cet

1
2
3
4
[root@cent8-02-jeremy gcc-build]# ../gcc-10.2.0/configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --disable-multilib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-initfini-array --enable-gnu-indirect-function --enable-cet
...
configure: creating ./config.status
config.status: creating Makefile

4.3 编译

编译过程很长,这边耗时53分46秒,且编译后文件很大,这边编译完文件夹大小6.6G,指令:
make -j8

1
2
3
4
5
[root@cent8-02-jeremy gcc-build]# make -j8
make[4]: Leaving directory '/home/jeremy/gcc-build/x86_64-pc-linux-gnu/libsanitizer'
make[3]: Leaving directory '/home/jeremy/gcc-build/x86_64-pc-linux-gnu/libsanitizer'
make[2]: Leaving directory '/home/jeremy/gcc-build/x86_64-pc-linux-gnu/libsanitizer'
make[1]: Leaving directory '/home/jeremy/gcc-build'

4.4 安装

make install

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@cent8-02-jeremy gcc-build]# make install
...
----------------------------------------------------------------------
Libraries have been installed in:
/usr/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.
----------------------------------------------------------------------
make[4]: Nothing to be done for 'install-data-am'.
make[4]: Leaving directory '/home/jeremy/gcc-build/x86_64-pc-linux-gnu/libatomic'
make[3]: Leaving directory '/home/jeremy/gcc-build/x86_64-pc-linux-gnu/libatomic'
make[2]: Leaving directory '/home/jeremy/gcc-build/x86_64-pc-linux-gnu/libatomic'
make[1]: Leaving directory '/home/jeremy/gcc-build'

至此,gcc10.2.0已成功编译安装。查看gcc信息,指令:
gcc -v

1
2
3
4
5
6
7
8
9
[root@cent8-02-jeremy gcc-build]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/10/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-10.2.0/configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --disable-multilib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-initfini-array --enable-gnu-indirect-function --enable-cet
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (GCC)

五、测试编译

编译简单的 hello C 程序

1
2
3
[root@cent8-02-jeremy c]# gcc -o hello hello.c
[root@cent8-02-jeremy c]# ./hello
hello world

六、杂谈

我总共编译了两次gcc-10.2.0,第一次我没有用contrib/download_prerequisites这个指令安装官方测试过的依赖:gmp-6.1.0、mpc-1.0.3、mpfrr-3.1.4 和 isl-0.18。我是自己下载的最新版本依赖:gmp-6.2.0,mpc-1.2.0,mpfr-4.1.0 和 isl-0.22.1。一开始找不到 static-libstdc++ 导致 gcc 配置失败。解决后,因为 isl-0.22.1 太新了,gcc 的 configure 没有配置正确,make 编译很久后,报以下异常:

1
#error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

后面研究了一阵子,通过编译 gcc 时顺便编译 isl 最新版本不可能,我就自己编译 isl-0.22.1。结果 isl 依赖 gmp ,gmp 依赖 m4,下载了 m4-1.4.18 最新版本,编译 m4 的时候,m4 需要 glibc,但是 glibc-2.28 代码修改了导致通过 glibc-2.28 版本无法顺利编译 m4,报以下异常:

1
#error "Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread, ungetc on your system, then report this to bug-gnulib."

又是花大量精力搜索,最后找到另外一个官方组织的 m4 git 仓库,里面有一次 commit 信息就是解决 glibc 新版代码变更问题。比对了一下修改,将自己在 m4 官网下载的 m4-1.4.18 的几个c文件对照着修改,最终编译成功 m4,之后的 gmp、isl 也顺利编过,然后 gcc 里面剩下 mpc 和 mpfr ,在 configure 的时候指定了 gmp 和 isl 的路径,顺利以全部最新版本编译成功 gcc。吐槽一下,一开始我用的一核虚拟机编译,足足花了4小时52秒才编译完。能解决还是蛮有成就感的。有空的话我会整理全最新依赖编译安装最新 gcc 的问题排查经过和思路,以及各种解决方案。

后面我想得出一个简便的方案,就最小化安装了一个 CentOS 8,并总结写了这篇文章。

七、附录

7.1 可选依赖

构建info信息是需要makeinfo,若没有,编译时不会构建info文档。

1
2
3
checking for makeinfo... /home/jeremy/gcc/gcc-10.2.0/missing makeinfo --split-size=5000000 --split-size=5000000
configure: WARNING:
*** Makeinfo is missing. Info documentation will not be built

可通过安装texinfo解决。指令:dnf install --enablerepo PowerTools texinfo -y

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
[jeremy@cent8-01-jeremy lib]$ sudo dnf install texinfo
Last metadata expiration check: 0:39:24 ago on Fri 04 Sep 2020 01:22:42 PM CST.
Dependencies resolved.
======================================================================================================
Package Arch Version Repository Size
======================================================================================================
Installing:
texinfo x86_64 6.5-6.el8 PowerTools 1.2 M
Installing dependencies:
perl-Class-Inspector noarch 1.32-2.el8 AppStream 37 k
perl-Digest noarch 1.17-395.el8 AppStream 27 k
perl-Digest-MD5 x86_64 2.55-396.el8 AppStream 37 k
perl-Net-SSLeay x86_64 1.88-1.el8 AppStream 379 k
perl-Text-Unidecode noarch 1.30-5.el8 AppStream 149 k
perl-URI noarch 1.73-3.el8 AppStream 116 k
perl-libintl-perl x86_64 1.29-2.el8 AppStream 817 k
perl-libnet noarch 3.11-3.el8 AppStream 121 k
perl-Carp noarch 1.42-396.el8 BaseOS 30 k
perl-Data-Dumper x86_64 2.167-399.el8 BaseOS 58 k
perl-Encode x86_64 4:2.97-3.el8 BaseOS 1.5 M
perl-Errno x86_64 1.28-416.el8 BaseOS 76 k
perl-Exporter noarch 5.72-396.el8 BaseOS 34 k
perl-File-Path noarch 2.15-2.el8 BaseOS 38 k
perl-File-Temp noarch 0.230.600-1.el8 BaseOS 63 k
perl-Getopt-Long noarch 1:2.50-4.el8 BaseOS 63 k
perl-HTTP-Tiny noarch 0.074-1.el8 BaseOS 58 k
perl-IO x86_64 1.38-416.el8 BaseOS 141 k
perl-MIME-Base64 x86_64 3.15-396.el8 BaseOS 31 k
perl-PathTools x86_64 3.74-1.el8 BaseOS 90 k
perl-Pod-Escapes noarch 1:1.07-395.el8 BaseOS 20 k
perl-Pod-Perldoc noarch 3.28-396.el8 BaseOS 86 k
perl-Pod-Simple noarch 1:3.35-395.el8 BaseOS 213 k
perl-Pod-Usage noarch 4:1.69-395.el8 BaseOS 34 k
perl-Scalar-List-Utils x86_64 3:1.49-2.el8 BaseOS 68 k
perl-Socket x86_64 4:2.027-3.el8 BaseOS 59 k
perl-Storable x86_64 1:3.11-3.el8 BaseOS 98 k
perl-Term-ANSIColor noarch 4.06-396.el8 BaseOS 46 k
perl-Term-Cap noarch 1.17-395.el8 BaseOS 23 k
perl-Text-ParseWords noarch 3.30-395.el8 BaseOS 18 k
perl-Text-Tabs+Wrap noarch 2013.0523-395.el8 BaseOS 24 k
perl-Time-Local noarch 1:1.280-1.el8 BaseOS 34 k
perl-Unicode-Normalize x86_64 1.25-396.el8 BaseOS 82 k
perl-constant noarch 1.33-396.el8 BaseOS 25 k
perl-interpreter x86_64 4:5.26.3-416.el8 BaseOS 6.3 M
perl-libs x86_64 4:5.26.3-416.el8 BaseOS 1.6 M
perl-macros x86_64 4:5.26.3-416.el8 BaseOS 72 k
perl-parent noarch 1:0.237-1.el8 BaseOS 20 k
perl-podlators noarch 4.11-1.el8 BaseOS 118 k
perl-threads x86_64 1:2.21-2.el8 BaseOS 61 k
perl-threads-shared x86_64 1.58-2.el8 BaseOS 48 k
perl-Unicode-EastAsianWidth noarch 1.33-13.el8 PowerTools 16 k
Installing weak dependencies:
perl-File-ShareDir noarch 1.104-3.el8 AppStream 29 k
perl-IO-Socket-IP noarch 0.39-5.el8 AppStream 47 k
perl-IO-Socket-SSL noarch 2.066-4.el8 AppStream 297 k
perl-Mozilla-CA noarch 20160104-7.el8 AppStream 15 k

Transaction Summary
======================================================================================================
Install 47 Packages

Total download size: 14 M
Installed size: 45 M

7.2 GCC 配置↩︎

1
2
3
4
5
6
7
8
9
10
[jeremy@cent8-02-jeremy gcc-build]$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)