CentOS 6.5 源码编译安装 GCC 7.2.0

一、安装包准备

所有的安装包可在http://mirror.team-cymru.com/gnu/找到。

GCC下载地址:GCC7.2.0

依赖包下载地址:mpfr-4.0.1mpc-1.0.3gmp-6.1.2

二、安装依赖项

使用yum安装的依赖项:yum install -y glibc-headers gcc-c++ automake autoconf

对于gmp, mpfr和mpc三个依赖包,如果想偷懒就使用sudo yum install -y gmp-devel mpfr-devel libmpc-devel安装,跳过下面的步骤。

2.1 安装gmp

wget http://mirror.team-cymru.com/gnu/gmp/gmp-6.1.2.tar.xz
tar -Jxvf gmp-6.1.2.tar.xz
cd gmp-6.1.2
./configure --prefix=/usr/local/gmp-6.1.2
make && make check
sudo make install
sudo ln -s /usr/local/gmp-6.1.2 /usr/local/gmp

2.2 安装mpfr

wget http://mirror.team-cymru.com/gnu/mpfr/mpfr-3.1.6.tar.xz
tar -Jxvf mpfr-3.1.6.tar.xz
cd mpfr-3.1.6
./configure --prefix=/usr/local/mpfr-3.1.6 --with-gmp=/usr/local/gmp
make && make check
sudo make install
sudo ln -s /usr/local/mpfr-3.1.6 /usr/local/mpfr

2.3 安装mpc

wget http://mirror.team-cymru.com/gnu/mpc/mpc-1.0.3.tar.gz
tar -zxvf mpc-1.0.3.tar.gz
cd mpc-1.0.3
./configure --prefix=/usr/local/mpc-1.0.3 --with-gmp=/usr/local/gmp --with-mpfr=/usr/local/mpfr
make && make check
sudo make install
sudo ln -s /usr/local/mpc-1.0.3 /usr/local/mpc

2.4 添加动态库

sudo echo "usr/local/mpc/lib" > /etc/ld.so.conf.d/gcc.conf
sudo echo "/usr/local/mpfr/lib" > /etc/ld.so.conf.d/gcc.conf
sudo echo "/usr/local/gmp/lib" > /etc/ld.so.conf.d/gcc.conf
sudo ldconfig

三、安装gcc

# 下载源码包
> wget http://ftp.gnu.org/gnu/gcc/gcc-7.2.0/gcc-7.2.0.tar.gz
> tar -zxf gcc-7.2.0.tar.gz
> cd gcc-7.2.0
> mkdir build && cd build  # 创建一个文件夹存放编译文件
# 配置configure
> ../configure --prefix=/usr/local/gcc7 --enable-languages=c,c++,go  --disable-multilib \
--with-gmp=/usr/local/gmp --with-mpc=/usr/local/mpc --with-mpfr=/usr/local/mpfr
# 编译并安装
> make && make install

mpc , mpfr , gmp 三个组件包也可以使用官方提供的安装脚本安装:./contrib/download_prerequisite

编译安装十分考验机器的配置,腾讯云1G1H GCC6.2 用时1小时,阿里云2G1H GCC7.2.0 用时2.5小时,本地虚拟机2G2H GCC7.2.0 用时4+小时。

编译安装完成后/usr/local/gcc7 目录下的文件结构:

> # ll
total 24
drwxr-xr-x. 2 root root 4096 Jan  7 10:44 bin
drwxr-xr-x. 3 root root 4096 Jan  7 10:44 include
drwxr-xr-x. 3 root root 4096 Jan  7 10:42 lib
drwxr-xr-x. 3 root root 4096 Jan  7 10:47 lib64
drwxr-xr-x. 3 root root 4096 Jan  7 10:42 libexec
drwxr-xr-x. 6 root root 4096 Jan  7 10:46 share

gccg++ 等可执行文件存放在bin 目录下,创建软链接即可使用。

# 备份原有的gcc和g++命令
> mv /usr/bin/gcc /usr/bin/gcc4
> mv /usr/bin/g++ /usr/bin/g++4
# 生成新的软链接
> ln -s /usr/local/gcc7/bin/gcc /usr/bin/gcc7
> ln -s /usr/local/gcc7/bin/g++ /usr/bin/g++
# 查看gcc版本
[root@localhost gcc7]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc7/libexec/gcc/x86_64-pc-linux-gnu/7.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --prefix=/usr/local/gcc7 --enable-languages=c,c++,go --disable-multilib
Thread model: posix
gcc version 7.2.0 (GCC)
# 查看g++版本
> g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/gcc7/libexec/gcc/x86_64-pc-linux-gnu/7.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --prefix=/usr/local/gcc7 --enable-languages=c,c++,go --disable-multilib
Thread model: posix
gcc version 7.2.0 (GCC)
最后修改:2018 年 01 月 07 日 06 : 24 PM

发表评论