出于需求系统自带的是gcc4.4.7,版本太低无法编译,被逼无奈只能升级gcc了,碰到很多坑,下载这个玩意就用了不少时间。
首先确保系统安装了gcc编译器(检测是否安装gcc最好的办法就是输入gcc命令即可,如果提示没有gcc命令,那就是未安装),如果没有,那么需要安装gcc先(系统默认安装的是老版本,这也是我们为什么升级gcc的原因)
#安装gcc、c++编译器以及内核文件
yum -y install gcc gcc-c++ kernel-devel
#安装C和C++的静态库
yum install -y glibc-static libstdc++-static
#安装旧版本的gcc、c++编译器
yum install gcc gcc-c++
为了防止后续安装缺少依赖包,建议安装开发常用工具包:
yum groupinstall "Development Tools"
做好了上述的准备工具,下面开始正式的升级过程:
1.下载源码包
wget http://ftp.gnu.org/gnu/gcc/gcc-4.8.0/gcc-4.8.0.tar.bz2 tar -jxvf gcc-4.8.0.tar.bz2
2.下载编译所需依赖库(此步骤非常重要,决定编译成功与否)
cd gcc-4.8.0 ./contrib/download_prerequisites cd ..
仔细看这一步骤,其实就是要下载一些依赖包,我们可以查看./contrib/download_prerequisites脚本,便知道主要是为了下载三个库:MPFR, GMP, MPC.
./contrib/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-4.8.0目录,分别执行如下命令:
tar xjf mpfr-2.4.2.tar.bz2 ln -sf mpfr-2.4.2 mpfr tar xjf gmp-4.3.2.tar.bz2 ln -sf gmp-4.3.2 gmp tar xzf mpc-0.8.1.tar.gz ln -sf mpc-0.8.1 mpc
执行完成后,便可以继续下一步了。
3.建立编译输出目录
mkdir gcc-build-4.8.0
4.进入此目录,执行以下命令,生成makefile文件
cd gcc-build-4.8.0 ../gcc-4.8.0/configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
5.编译
# j 后面的是核心数,编译速度会比较快 make -j4
编译过程会遇到如下问题:
make[3]: Leaving directory `/root/cpp_mongodb/gcc-4.8.2/gcc-build-4.8.2/build-x86_64-unknown-linux-gnu/libiberty'
make[2]: Leaving directory `/root/cpp_mongodb/gcc-4.8.2/gcc-build-4.8.2'
make[1]: *** [stage1-bubble] 错误 2
make[1]: Leaving directory `/root/cpp_mongodb/gcc-4.8.2/gcc-build-4.8.2'
make: *** [all] 错误 2
那么需要做的是安装依赖包:
yum -y install glibc-devel.i686 glibc-devel yum install gcc-c++ yum groupinstall "Development Tools"
安装依赖库完成之后,需要重新配置,重新编译。
6.安装
sudo make install
7.切换GCC到新版
确定新安装的GCC的路径,一般默认在/usr/local/bin下。可以先updatedb,然后locate gcc-4.8|tail找一下
ls /usr/local/bin | grep gcc
添加新GCC到可选项,倒数第三个是名字,倒数第二个参数为新GCC路径,最后一个参数40为优先级,设大一些之后就自动使用新版了
update-alternatives --install /usr/bin/gcc gcc /usr/local/bin/i686-pc-linux-gnu-gcc 40
8.确认当前版本已经切换为新版.
gcc -v
文章的脚注信息由WordPress的wp-posturl插件自动生成
来自外部的引用: 1