CentOS 6 编译安装gcc 5.0+

2018-06-15  本文已影响0人  长江十三寨总瓢把子

CentOS6.5默认安装的gcc版本是4.4.7,而处于某些软件的编译需求,gcc版本要求在5.0以上,因此只能DIY了。

1. 环境准备

我自己本地配了个CentOS6.5的虚机,配置好yum源(方法参考:https://www.jianshu.com/p/422813bdd34c)。

yum install gcc
yum install gcc-c++
tar -xzvf gcc-5.1.0.tar.gz
cd gcc-5.1.0

2. 下载依赖

gcc的源码中有个依赖的下载脚本,如果虚机网络可以正常访问外网,可以直接执行脚本

cat ./contrib/download_prerequisites
GRAPHITE_LOOP_OPT=yes

# Necessary to build GCC.
MPFR=mpfr-2.4.2
GMP=gmp-4.3.2
MPC=mpc-0.8.1
...

首先执行下载脚本,报错,DNS解析有问题

[root@localhost contrib]# ./download_prerequisites 
--2018-06-15 09:59:24--  ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2
           => “mpfr-2.4.2.tar.bz2”
Resolving gcc.gnu.org... failed: Name or service not known.
wget: unable to resolve host address “gcc.gnu.org”

尝试解决,但是我的虚机是公司内网,无奈只能手动下载依赖的包了

vim /etc/resolv.conf
nameserver 8.8.8.8 #google域名服务器
nameserver 8.8.4.4 #google域名服务器

下载下来的依赖包放到gcc源码的contrib目录下,然后编辑download_prerequisites脚本,把下载的命令全部注释掉,重新执行一次。

3. 编译安装

第一次编译报错

./configure --prefix=/usr/gcc --enable-languages=c,c++
...
configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.
Try the   options to specify
their locations.  Source code for these libraries can be found at
their respective hosting sites as well as at
ftp://gcc.gnu.org/pub/gcc/infrastructure/.  See also
http://gcc.gnu.org/install/prerequisites.html for additional info.  If
you obtained GMP, MPFR and/or MPC from a vendor distribution package,
make sure that you have installed both the libraries and the header
files.  They may be located in separate packages.

添加--with-gmp --with-mpfr --with-mpc之后再次尝试,报同样的错误,似乎gcc并不会自动编译安装这些依赖包,尝试先手动安装。

3.1 编译安装GMP

cd /opt/gcc-5.1.0/contrib/gmp
./configure --prefix=/usr/gmp-4.3.2
make && make install

3.2 编译安装MPFR

cd /opt/gcc-5.1.0/contrib/mpfr-2.4.2
./configure --prefix=/usr/mpfr-2.4.2 --with-gmp=/usr/gmp-4.3.2
make && make install

3.3 编译安装MPC

cd /opt/gcc-5.1.0/contrib/mpc-0.8.1
./configure --prefix=/usr/mpc-0.8.1 --with-gmp=/usr/gmp-4.3.2 --with-mpfr=/usr/mpfr-2.4.2 
make && make install

然后配置lib环境变量

export LD_LIBRARY_PATH=/usr/gmp-4.3.2/lib:/usr/mpc-0.8.1/lib/:/usr/mpfr-2.4.2/lib:$LD_LIBRARY_PATH

再次尝试编译gcc

./configure --prefix=/usr/gcc --enable-languages=c,c++ --with-gmp=/usr/gmp-4.3.2 --with-mpfr=/usr/mpfr-2.4.2 --with-mpc=/usr/mpc-0.8.1
...
checking for compatible ISL... no
*** This configuration is not supported in the following subdirectories:
     target-libmpx gnattools gotools target-libada target-libgfortran target-libgo target-libffi target-libbacktrace target-zlib target-libjava target-libobjc target-liboffloadmic target-boehm-gc
    (Any other directories should still work fine.)
checking for default BUILD_CONFIG... bootstrap-debug
checking for --enable-vtable-verify... no
/usr/bin/ld: crt1.o: No such file: No such file or directory
collect2: ld returned 1 exit status
configure: error: I suspect your system does not have 32-bit developement libraries (libc and headers). If you have them, rerun configure with --enable-multilib. If you do not have them, and want to build a 64-bit-only compiler, rerun configure with --disable-multilib.

缺少32位的依赖包,但是我只想编译64位的,因此添加--disable-multilib编译选项,再次尝试,成功。最后一步就是makemake install了。比较漫长。。。

上一篇下一篇

猜你喜欢

热点阅读