缘由

Google 很难找到 CentOS 7平台,  版本高于 4.8的gcc.
某个源码 编译要求 gcc 版本 需高于 gcc 4.8

目标:

1. 在CentOS 7.4 平台, 编译安装 GCC 7.5
2. 在离线环境编译 安装 GCC

准备:

下载必要的依赖文件:

1
2
3
4
5
root@localhost:~# wget https://ftp.gnu.org/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.xz
root@localhost:~# wget https://ftp.gnu.org/gnu/gmp/gmp-6.1.0.tar.bz2
root@localhost:~# wget https://ftp.gnu.org/gnu/mpfr/mpfr-3.1.4.tar.bz2
root@localhost:~# wget https://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz ^C
root@localhost:~# wget https://gcc.gnu.org/pub/gcc/infrastructure/isl-0.16.1.tar.bz2

解压

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
root@localhost:~# tar xf gcc-7.5.0.tar.xz
root@localhost:~# cd gcc-7.5.0

root@localhost:gcc-7.5.0# #gcc 编译期间的依赖组件
root@localhost:gcc-7.5.0# tar xf gmp-6.1.0.tar.bz2
root@localhost:gcc-7.5.0# tar xf mpfr-3.1.4.tar.bz2
root@localhost:gcc-7.5.0# tar xf mpc-1.0.3.tar.gz
root@localhost:gcc-7.5.0# tar xf isl-0.16.1.tar.bz2

root@localhost:gcc-7.5.0# #设置 依赖组件 软连接
root@localhost:gcc-7.5.0# ln -fs gmp-6.1.0 gmp
root@localhost:gcc-7.5.0# ln -fs mpfr-3.1.4 mpfr
root@localhost:gcc-7.5.0# ln -fs mpc-1.0.3 mpc
root@localhost:gcc-7.5.0# ln -fs isl-0.16.1 isl
root@localhost:gcc-7.5.0#

开始编译

1
2
3
root@localhost:gcc-7.5.0# ./configure --prefix=/opt/gcc_7.5 --enable-checking=release --enable-languages=c,c++ --disable-multilib
root@localhost:gcc-7.5.0# make #单核编译 约40分钟
root@localhost:gcc-7.5.0# make install

此时, 系统就有另个版本的GCC 了

配置新版本GCC:

1
2
root@localhost:~# vim ~/.bashrc
export PATH=/opt/gcc_7.5/bin/:$PATH

验证:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
root@localhost:~# source ~/.bashrc  #重新读取环境变量
root@localhost:~# logout #或者 重新登录

[root@localhost ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/gcc_7.5/libexec/gcc/x86_64-pc-linux-gnu/7.5.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --prefix=/opt/gcc_7.5 --enable-checking=release --enable-languages=c,c++ --disable-multilib
Thread model: posix
gcc version 7.5.0 (GCC)
[root@localhost ~]#

[root@localhost ~]# gcc -l pthread
/lib/../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
[root@localhost ~]#