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

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 编译gcc-4.6.3 准备 下载GCC 源码包 : gcc-4.6.3.tar.bz2 下载GCC 依赖包:...
    火星来的农民阅读 6,078评论 0 1
  • 2014-05-26 21:21:38构建Linux 编译Linux BeagleBone Black 14年即将...
    外星间谍阅读 8,866评论 0 9
  • 介绍 GCC 全称是 GNU Compiler Collection。 GCC 原本是指 C 语言的编译器,后来逐...
    juniway阅读 10,580评论 1 1
  • 如果你拥有最高权限,如果你只管理一台服务器,那么系统自带的包管理工具就帮你解决了所有问题。但是真实世界没有那么美好...
    xuzhougeng阅读 13,710评论 4 30
  • 夜雨,入秋,微凉。 今天早上终于吃到了传说中的邱记小吃,算起来,怕是第四次去碰运气了吧?刘皇叔为了江山社稷,付出的...
    雨天仁兄阅读 1,274评论 0 0

友情链接更多精彩内容