天天看点

【Tools】gcc4.4升级到gcc4.8

00.目录

  • 00.目录
  • 01. 简介
  • 02. 软件包下载
  • 03. 安装gmp
  • 04. 安装mpfr
  • 05. 安装mpc
  • 06. 安装gcc
  • 07. 错误排查(可选 )

01. 简介

由于gcc4.4不支持C++11新特性,所以将Redhat6.5中gcc4.4升级到gcc4.8. 本文介绍在系统无法连接互联网的情况下,如何升级GCC。离线和在线升级的主要区别在于,如果可以联网,在升级gcc前的需要安装的依赖包,可以通过运行gcc安装包下的脚本自行下载安装,免去了很多的麻烦。

环境:RedHat6.5

首先下载gcc安装包,本文为gcc-4.8.5.tar.gz,解压后,如果直接运行安装目录下的configure脚本,可能会因为当前系统的GMP,MPFR,MPC的版本过低而抛出如下的错误

【Tools】gcc4.4升级到gcc4.8

02. 软件包下载

需要使用的安装包为 gcc-4.8.5.tar.gz,gmp-5.0.5.tar.bz2,mpfr-3.0.1.tar.gz,mpc-1.0.1.tar.gz.

如果是在联网环境,可以运行安装目录下的”./contrib/download_prerequisites”脚本来下载相关的依赖。而由于我们是在局域网内,所以需要到因特网中下载这三个安装包,然后逐个安装:

download_prerequisites脚本内容如下:

#! /bin/sh

# Download some prerequisites needed by gcc.
# Run this from the top level of the gcc source tree and the gcc
# build will do the right thing.
#
# (C) 2010 Free Software Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.

MPFR=mpfr-2.4.2
GMP=gmp-4.3.2
MPC=mpc-0.8.1

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPFR.tar.bz2 || exit 1
tar xjf $MPFR.tar.bz2 || exit 1
ln -sf $MPFR mpfr || exit 1

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$GMP.tar.bz2 || exit 1
tar xjf $GMP.tar.bz2  || exit 1
ln -sf $GMP gmp || exit 1

wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPC.tar.gz || exit 1
tar xzf $MPC.tar.gz || exit 1
ln -sf $MPC mpc || exit 1

rm $MPFR.tar.bz2 $GMP.tar.bz2 $MPC.tar.gz || exit 1      

下载链接:

gcc下载链接:gcc-4.8.5下载

gmp下载链接:gmp-5.0.5下载

mpfr下载链接:mpfr-3.0下载

mpc下载链接: mpc-1.0.1下载

03. 安装gmp

GMP是一个任意精度的开源算术库,可用于符号整数,有理数,浮点数计算。

第一步: 解压

[root@deng tmp]# tar -xjvf gmp-5.0.5.tar.bz2

第二步: 检查环境,生成对应文件

[root@deng tmp]# cd gmp-5.0.5

[root@deng gmp-5.0.5]# ./configure

第三步: 编译

[root@deng gmp-5.0.5]# make -j4

[root@deng gmp-5.0.5]# make check

第四步: 安装

[root@deng gmp-5.0.5]# make install

04. 安装mpfr

mpfr主要为提供C/C++多精度浮点运算

第一步: 解压

[root@deng tmp]# tar -xjvf mpfr-3.0.1.tar.bz2

第二步: 检查环境

[root@deng tmp]# cd mpfr-3.0.1

[root@deng mpfr-3.0.1]# ./configure –with-gmp-include=/usr/local/include –with-gmp-lib=/usr/local/lib

第三步: 编译

[root@deng mpfr-3.0.1]# make -j4

[root@deng mpfr-3.0.1]# make check

第四步: 安装

[root@deng mpfr-3.0.1]# make install

05. 安装mpc

第一步: 解压

[root@deng tmp]# tar -xzvf mpc-1.0.1.tar.gz

第二步: 检查环境

[root@deng tmp]# cd mpc-1.0.1

[root@deng mpc-1.0.1]# ./configure

第三步: 编译

[root@deng mpc-1.0.1]# make -j4

第四步: 安装

[root@deng mpc-1.0.1]# make install

第五步: 配置

安装后,它们的头文件位于”/usr/local/include”,默认情况下程序可以自动找到该路径;它们的动态库位于”/usr/local/lib”,可在环境变量追加该路径,此处就在当前用户的环境变量上加上该路径:

[root@deng tmp]# vim ~/.bash_profile

添加如下内容:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/lib64:/usr/lib64

使配置生效

[root@deng tmp]# source ~/.bash_profile
【Tools】gcc4.4升级到gcc4.8

06. 安装gcc

第一步: 解压

[root@deng tmp]# tar -xzvf gcc-4.8.5.tar.gz

第二步: 检查环境

[root@deng tmp]# cd gcc-4.8.5

[root@deng gcc-4.8.5]# mkdir gcc-4.8.5-build

[root@deng gcc-4.8.5]# cd gcc-4.8.5-build/

可以参考gcc -v选项 注意: 如果此处出现关于java的错误, 可以将Java选项去掉

【Tools】gcc4.4升级到gcc4.8
[root@deng gcc-4.8.5-build]# ../configure –prefix=/usr –mandir=/usr/share/man –infodir=/usr/share/info –disable-multilib –enable-bootstrap –enable-shared –enable-threads=posix –enable-checking=release –with-system-zlib –enable-__cxa_atexit –disable-libunwind-exceptions –enable-gnu-unique-object –enable-languages=c,c++, –disable-dssi –disable-libjava-multilib –with-ppl –with-cloog –with-tune=generic –with-arch_32=i686 –build=x86_64-redhat-linux
【Tools】gcc4.4升级到gcc4.8

第三步: 编译

[root@deng gcc-4.8.5-build]# make -j4

第四步: 安装

[root@deng gcc-4.8.5-build]# make install

第五步: 测试

gcc测试

【Tools】gcc4.4升级到gcc4.8

g++测试

【Tools】gcc4.4升级到gcc4.8

验证安装

除了可以通过”gcc -v”查看安装后的gcc版本,还可以通过编写C++11标准的程序来验证,在编译C++11程序时,应该加上”std=c++11”,否则默认是以C99进行编译,将会抛出错误。

07. 错误排查(可选 )

​如果06出现老版本 就可以参考一下操作​

安装完成后,系统默认没有修改环境变量,目前还是使用老版本的gcc。

设置使用新版gcc:

ls /usr/local/bin | grep gcc 添加新GCC到可选项,倒数第三个是名字,倒数第二个参数为新GCC路径,最后一个参数40为优先级,设大一些之后就自动使用新版了

update-alternatives –install /usr/bin/gcc gcc /usr/local/gcc4.8/bin/i686-pc-linux-gnu-gcc 40

第二种方式: 创建软连接

mkdir /usr/gcc447backup/

mv /usr/bin/{gcc,g++} /usr/gcc447backup

ln -s /usr/local/gcc4.8/bin/gcc /usr/bin/gcc

ln -s /usr/local/gcc4.8/bin/g++ /usr/bin/g++