CentOS 6 编译安装gcc 5.0+

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

1. 环境准备

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

  • 编译gcc之前,首先需要环境中已经安装一套gcc和g++

 

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

 

3.4 再次尝试编译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了。比较漫长。。。



作者:yglh_gq
链接:https://www.jianshu.com/p/89da07e117f0
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

=========装完gcc后,也许不能编译,需要手动修复一些东西=========

1、按照上述教程,会安装到3.4步骤里面指定的/usr/gcc里面(这个文件夹可以压缩打包,拷贝到别的环境也能使用,但也要修复一些东西)

2、现在直接还无法用gcc-5.1.0来编译,需要把安装路径下的bin文件加到环境变量,或者把bin文件拷贝到/usr/bin下面不改动环境变量。

如 ln -s /usr/gcc/bin/gcc /usr/bin/gcc-5  ; ln -s /usr/gcc/bin/g++ /usr/bin/g++-5

创建软链接这样子,在/usr/bin下面就有了gcc-5,可以用gcc-5 hello.c来编译了

 

3、usr/gcc/libexec/gcc/x86_64-unknown-linux-gnu/5.1.0/cc1plus下面的3个lib库无法识别,需要修复

        libmpc.so.2 => not found
        libmpfr.so.1 => not found
        libgmp.so.3 => not found

把3.1,3.2, 3.3步骤里安装路径下lib文件夹里的库文件,拷贝到/usr/lib64下

比如 cp /usr/gmp-4.3.2/lib/libgmp.* /usr/lib64/

 

4、这个时候已经可以用gcc-5编译了,但运行时候会报错

./a.out: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./a.out)

需要把安装路径下的gcc/lib64/libstdc++.so.6.0.21拷过去。让本来的libstdc++.so.6指向新版本的so.6.0.21

比如 ln -sf /usr/gcc/lib64/libstdc++.so.6.0.21   /usr/lib64/libstdc++.so.6

 

现在,就可以正常编译运行了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值