操作系统是redhat x86_64
需要准备gcc源代码包
gcc-4.6.0.tar.bz2 http://gcc.gnu.org or ftp://gcc.gnu.org
首先准备三个库,这三个库都是高精度大数计算库。
gmp-5.0.2 or higher http://gmplib.org/
mpc-0.9 or higher http://www.multiprecision.org/index.php?prog=mpc
mpfr-3.0.1 or higher http://www.mpfr.org
下载的压缩包需要解压,如果是bz2就用tar -xvjf,如果是gz就用tar -xvzf,假设安装包全部下载在/home/gogdizzy下。
cd /home/gogdizzy tar -xvjf gmp-5.0.2.tar.bz2 tar -xvjf mpfr-3.0.1.tar.bz2 tar -xvzf mpc-0.9.tar.gz tar -xvjf gcc-4.6.0.tar.bz2
因为mpc依赖gmp和mpfr,所以最后处理mpc。
安装gmp库
cd gmp-5.0.2 ./configure --prefix=/usr/local/lib/gmp-5.0.2/ make && make install
安装mpfr库
cd ../mpfr-3.0.1 ./configure --prefix=/usr/local/lib/mpfr-3.0.1/ make && make install
安装mpc库
cd ../mpc-0.9 ./configure --prefix=/usr/local/lib/mpc-0.9/ --with-mpfr=/usr/local/lib/mpfr-3.0.1/ --with-gmp=/usr/local/lib/gmp-5.0.2/ make && make install
配置这三个库的库文件路径(/etc/ld.so.conf内容是include ld.so.conf.d/*.conf,所以我们在ld.so.conf.d中添加一个文件)
cd /etc/ld.so.conf.d cat >gcc-4.6.0.conf <<EXIT > /usr/local/lib/gmp-5.0.2/lib > /usr/local/lib/mpfr-3.0.1/lib > /usr/local/lib/mpc-0.9/lib > EXIT ldconfig
这样,路径就被编译进/etc/ld.so.cache里面了,将来我们的程序编译连接的时候会用到它。
下面编译安装gcc-4.6.0,由于编译漫长,并且可能因为配置不好,编译失败,所以要新建一个文件夹保存编译结果,一旦编译失败,就直接将编译结果全部rm掉,重新配置。
编译的配置选项众多,详细介绍(http://gcc.gnu.org/install/configure.html):
cd /home/gogdizzy mkdir gcc-4.6.0-build cd gcc-4.6.0-build ../gcc-4.6.0/configure --prefix=/usr/lib/gcc/x86_64-redhat-linux/4.6.0/ --with-gmp=/usr/local/lib/gmp-5.0.2/ --with-mpfr=/usr/local/lib/mpfr-3.0.1/ --with-mpc=/usr/local/lib/mpc-0.9/ --mandir=/usr/sharegcc-4.6.0/man --infodir=/usr/share/gcc-4.6.0/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,go --disable-dssi --enable-plugin --with-cpu=generic make && make install
说明:我指定--host=x86_64-redhat-linux编译到一半出现错误,出现一个变量无法枚举的错误,后来干脆不指定host,默认是x86_64-unknown-linux,反而编译成功了。
这时输入gcc -v发现还是老版本的,这是因为默认调用的是/usr/bin/gcc,而我们安装的其实在/usr/lib/gcc/x86_64-redhat-linux/4.6.0/下,为了可以用新的gcc编译器。需要将gcc重新做个软链
cd /usr/bin/ mv gcc gcc.bak mv g++ g++.bak ln -s /usr/lib/gcc/x86_64-redhat-linux/4.6.0/bin/gcc gcc ln -s /usr/lib/gcc/x86_64-redhat-linux/4.6.0/bin/g++ g++
这时候运行gcc -v发现已经是新版本了,随便写一个cpp程序,编译也没有问题,然而运行时会出现问题。因为运行时涉及到动态库libstdc++.so.6,默认会去/usr/lib或/usr/lib64里去找,然而这里的还是老的库,版本不匹配。需要将新的库更新过去
cd /usr/lib/gcc/x86_64-redhat-linux/4.6.0/lib mv ./* /usr/lib cd /usr/lib/gcc/x86_64-redhat-linux/4.6.0/lib64 mv ./* /usr/lib64
好了,终于运行成功了,不过后面替换lib的操作都是我自己试验的,不知道是否全面或可靠,并且如果想恢复旧版本的,需要先备份/usr/lib和/usr/lib64下的同名文件,大家一定要注意!
参考文档:
http://www.linuxidc.com/Linux/2011-05/35364.htm