linux下gcc安装
1、准备工作
等原因,则需要安装最新版gcc。
先查看已有的gcc版本
如果系统还没有任何gcc被安装,则需要从开发包中先安装默认的低版本的gcc
centos下也可以安装带gcc的开发工具:yum groupinstall "Development Tools"
若不先安装旧版本的话安装可能会出现如下错误:
-
//=======================================
-
configure: error: no acceptable C compiler found in $PATH
-
//=======================================
2、下载
gcc下载地址
ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc
gcc在下面的 releases 文件夹内,目前最新版为 4.8.2
若直接安装会出现如下错误:
-
//=============================出现错误=====================================
-
configure: error: Building GCC requires GMP
4.2+, MPFR
2.3
.1+
and MPC
0.8
.0+.
-
Try the --with-gmp, --with-mpfr
and/
or --with-mpc options to specify
-
their locations.
-
//==========================================================================
因为缺少gcc依赖的gmp、mpfr、mpc三个软件,所以需要下载安装他们,他们在上面的
infrastructure 文件夹内可以找到。同时gmp与mpfr及mpc之间还有相互依赖关系,所以
要按下面顺序安装,并在 configure后面的选项后面带上所依赖软件的路径
下载好所有文件及解压后如下:
3、安装及错误解决
-
//1、解压gcc
-
$tar -jxvf gcc
-4.8
.2.tar.bz2
-
$cd gcc
-4.8
.2
-
-
//2、解压安装gmp
-
$tar -jxvf gmp
-4.3
.2.tar.bz2
-
$cd gmp
-4.3
.2
-
$./configure --prefix=/usr/local/gmp
-4.3
.2
//gmp安装路径
-
$make
-
$make check
-
$sudo make install
-
-
//3、解压安装mpfr
-
$tar -jxvf mpfr
-2.4
.2.tar.bz2
-
$cd mpfr
-2.4
.2
-
//congfigure后面是mpfr安装路径及依赖的gmp路径
-
$configure --prefix=/usr/local/mpfr
-2.4
.2 --with-gmp=/usr/local/gmp
-4.3
.2
-
$make
-
$make check
-
$sudo make install
-
-
//4、解压安装mpc
-
$tar -zxvf mpc
-0.8
.1.tar.gz
-
$cd mpc
-0.8
.1
-
//congfigure后面是mpc安装路径及依赖的gmp和mpfr路径
-
$configure --prefix=/usr/local/mpc
-0.8
.1
-
--with-gmp=/usr/local/gmp
-4.3
.2 --with-mpfr=/usr/local/mpfr
-2.4
.2
-
$make
-
$make check
-
$sudo make install
-
-
-
若没有下面的配置,则会出现如下的mpc库错误及其他库错误等等,所以需要进行一系列配置
-
-
//================================出现错误======================================
-
gcc error
while loading shared libraries: libmpc.so
.2:
-
cannot open shared object file:No such file
or directory
-
//===============================================================================
-
-
//5、配置库路径
-
-
#vi /etc/profile
//向/etc/profile文件末尾添加下面的语句(root权限下):
-
-
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/gmp
-4.3
.2/lib:
-
/usr/local/mpfr
-2.4
.2/lib:/usr/local/mpc
-0.8
.1/lib
-
-
#source /etc/profile
//使其立即生效
-
-
#echo $LD_LIBRARY_PATH
//查看配置是否成功
-
-
/usr/local/gmp
-4.3
.2/lib:/usr/local/mpfr
-2.4
.2/lib:/usr/local/mpc
-0.8
.1/lib
//显示这个表示成功
-
-
-
#vi /etc/ld.so.conf
//编辑这个文件,添加下面路径
-
-
/usr/local/mpc
-0.8
.1/lib
-
/usr/local/gmp
-4.3
.2/lib
-
/usr/local/mpfr
-2.4
.2/lib
-
-
$sudo ldconfig
-
-
-
-
//6、安装gcc
-
$./configure --prefix=/usr/local/gcc4
.8
.2 --enable-threads=posix --disable-checking --disable-multilib
-
--enable-languages=c,c++ --with-gmp=/usr/local/gmp
-4.3
.2 --with-mpfr=/usr/local/mpfr
-2.4
.2 --with-mpc=/usr/local/mpc
-0.8
.1
-
-
-
$make
-
$sudo make install
-
-
//7、配置gcc
-
#vi /etc/profile
//向/etc/profile文件末尾添加下面的语句(root权限下):
-
-
export PATH=$PATH:/usr/local/gcc4
.8
.2
-
-
#source /etc/profile
//使其立即生效
-
-
#rm /usr/bin/gcc
//删除旧的软连接
-
#ln -s /usr/local/gcc4
.8
.2/bin/gcc /usr/bin/gcc
//使新版本建立软连接
-
-
//下面的同理
-
#rm /usr/bin/g++
-
#ln -s /usr/local/gcc4
.8
.2/bin/g++ /usr/bin/g++
4、最后
查看是否安装更新成功:
这样最终就安装更新成功了,安装过程时间不短,破费功夫,若依赖文件及配置等处理的好的话会快些,过程中会出现很多错误, 一定要耐心解决。
一般命令的话在一般用户下即可解决($),需要 root 权限的,可以用 su 切换到 root 下 (#),或者在一般用户下,命令前加 sudo ,
关于 sudo 可以查看:
http://blog.csdn.net/wtfmonking/article/details/15494295
5、其他问题
如果不小心误删了 /usr/bin/gcc ,可以先删除旧版 gcc,在从软件包重新安装旧版,
最后再按上面的步骤安装新版。centos下可以:
-
$sudo yum remove gcc-* -y
-
$sudo yum groupinstall
"Development Tools"