CentOS 升级GCC教程

分类:CentOS教程 阅读:89453 次

本文转载自:http://wgkgood.blog.51cto.com/,对作者深表感谢!

最近由于需求的原因,需要把目前gcc-4.4.4升级到gcc-4.6.1,考虑到有多台的原因,做了一个脚本自动安装结合批量工具分发。

一、安装环境
系统版本:centos6.0X64
原GCC版本:gcc-4.4.4
新GCC版本:gcc-4.6.1

二、正式安装
1、下载安装的源码:

  1. 如下需要下载以下包:
  2. gcc-4.6.1.tar.bz2gmp-4.3.2.tar.bz2mpc-0.8.1.tar.gzmpfr-2.4.2.tar.bz2
  3. 下载地址如下:
  4. wgetftp://gcc.gnu.org/pub/gcc/infrastructure/{gmp-4.3.2.tar.bz2,mpc-0.8.1.tar.gz,mpfr-2.4.2.tar.bz2}
  5. wgethttp://ftp.gnu.org/gnu/gcc/gcc-4.6.1/gcc-4.6.1.tar.bz2
  6. 即可!

2、解压安装:安装有先后顺序:GMP、MPFR、MPC、GCC

1.安装GMP:

  1. tarjxfgmp-4.3.2.tar.bz2&&cdgmp-4.3.2/;./configure–prefix=/usr/local/gmp/&&make&&makeinstall

2.安装MPFR:

  1. tarjxfmpfr-2.4.2.tar.bz2;cdmpfr-2.4.2/;./configure–prefix=/usr/local/mpfr–with-gmp=/usr/local/gmp&&make&&makeinstall

3.安装MPC:

  1. tarxzfmpc-0.8.1.tar.gz;cdmpc-0.8.1;./configure–prefix=/usr/local/mpc–with-mpfr=/usr/local/mpfr–with-gmp=/usr/local/gmp&&make&&makeinstall

然后执行如下:

  1. exportLD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc/lib:/usr/local/gmp/lib:/usr/local/mpfr/lib/</span>

//这一步很关键,设置变量,不设置后面会报错。

4.安装GCC:

  1. tarjxfgcc-4.6.1.tar.bz2;cdgcc-4.6.1;./configure–prefix=/usr/local/gcc–enable-threads=posix–disable-checking–disable-multilib–enable-languages=c,c++–with-gmp=/usr/local/gmp–with-mpfr=/usr/local/mpfr/–with-mpc=/usr/local/mpc/&&make&&makeinstall
  1. 安装完毕即可!然后做一个链接就可以使用GCC了
  2. mkdir-p/data/backup/`date+%Y%m%d`
  3. mv/usr/bin/{gcc,g++}/data/backup/`date+%Y%m%d`
  4. 新建软链接:
  5. ln-s/usr/local/gcc/bin/gcc/usr/bin/gcc
  6. ln-s/usr/local/gcc/bin/g++/usr/bin/g++

三、测试GCC
直接输入gcc -v 可以查看到gcc 版本为4.6.1和相关的编译参数就ok了!附上一个最简单的安装脚本:

  1. #!/bin/sh
  2. ##automakeinstallgcc
  3. ##2012-07-03
  4. tarjxfgmp-4.3.2.tar.bz2&&cdgmp-4.3.2/;./configure–prefix=/usr/local/gmp/&&make&&makeinstall
  5. sleep1
  6. cd../;tarjxfmpfr-2.4.2.tar.bz2;cdmpfr-2.4.2/;./configure–prefix=/usr/local/mpfr–with-gmp=/usr/local/gmp&&make&&makeinstall
  7. cd../;tarxzfmpc-0.8.1.tar.gz;cdmpc-0.8.1;./configure–prefix=/usr/local/mpc–with-mpfr=/usr/local/mpfr–with-gmp=/usr/local/gmp&&make&&makeinstall
  8. cd../;tarjxfgcc-4.6.1.tar.bz2;cdgcc-4.6.1;./configure–prefix=/usr/local/gcc–enable-threads=posix–disable-checking–disable-multilib–enable-languages=c,c++–with-gmp=/usr/local/gmp–with-mpfr=/usr/local/mpfr/–with-mpc=/usr/local/mpc/
  9. if
  10. [$?-eq0];then
  11. echo“Thisgccconfigureissuccess”
  12. else
  13. echo“Thisgccconfigureisfailed”
  14. fi
  15. exportLD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc/lib:/usr/local/gmp/lib:/usr/local/mpfr/lib/
  16. make&&makeinstall
  17. [$?-eq0]&&echoThisismakeinstallsuccess

////--------------------------------------------------------------------------------------------------------------------------------------------------------------

configure: error: cannot compute suffix of object files: cannot compile

Indeed, adding the MPFR and GPM lib directories to LD_LIBRARY_PATH solves theproblem. For some reason I thought configure would handle this for me since Igave it --with-gmp and --with-mpfr settings. Would have been nice if configuretested for this and given a helpful error. In any event I'll close this out asan invalid bug report.

需要安装gmp与mpfr并且需要在.bash_profile(一般在/root目录下)配置LD_LIBRARY_PATH,把GMP与MPFR对应的lib配置上如:LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpfr/2.4.0/lib:/usr/local/gmp/4.2.2/lib
export LD_LIBRARY_PATH一定要重启生效或者使用 source /root/.bash_profile

error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file

1、找到libXXX.so.X的路径,比如/usr/local/xxx/lib

2、cd /etc

3、gedit ld.so.conf

4、添加/usr/local/xxx/lib

5、ldconfig