在Ubuntu系统上手动安装GCC环境

Ubuntu系统是自带GCC安装指令的apt install gcc,当前apt源中gcc版本为5.4.0,版本太低,推荐手动安装gcc8.3.0

手动安装gcc8.3.0之前需要先确保安装gcc环境依赖GMP 4.2+ 、 MPFR 2.3.1+ 、 MPC 0.8.0+,否则会报出以下错误

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.

安装GMP 6.1.2

#下载GMP安装包
 wget http://mirror.hust.edu.cn/gnu/gmp/gmp-6.1.2.tar.xz
#解压GMP安装包
 tar -Jxf gmp-6.1.2.tar.xz
#创建GMP安装路径
 mkdir -p /usr/local/gmp-6.1.2
#配置安装文件
 cd gmp-6.1.2
 ./configure --prefix=/usr/local/gmp-6.1.2
#编译源码
 make
#安装
 make install

安装MPFR4.0.2

#下载MPFR 安装包
 wget http://mirror.hust.edu.cn/gnu/mpfr/mpfr-4.0.2.tar.xz
#解压MPFR安装包
 tar -Jxf mpfr-4.0.2.tar.xz
#创建MPFR安装路径
 mkdir -p /usr/local/mpfr-4.0.2
#配置安装文件
 cd mpfr-4.0.2
 ./configure --prefix=/usr/local/mpfr-4.0.2  --with-gmp=/usr/local/gmp-6.1.2
#编译源码
 make
#安装
 make install

安装MPC1.1.0

#下载MPC 安装包
 wget http://mirror.hust.edu.cn/gnu/mpc/mpc-1.1.0.tar.gz
#解压MPC安装包
 tar -zxf mpc-1.1.0.tar.gz
#创建MPC安装路径
 mkdir -p /usr/local/mpc-1.1.0
#配置安装文件
 cd mpc-1.1.0
 ./configure --prefix=/usr/local/mpc-1.1.0  --with-gmp=/usr/local/gmp-6.1.2 --with-mpfr=/usr/local/mpfr-4.0.2
#编译源码
 make
#安装
 make install

上述全部安装完成后,安装GCC8.3.0

#下载gcc安装包
 wget mirror.hust.edu.cn/gnu/gcc/gcc-8.3.0/gcc-8.3.0.tar.gz
#解压gcc安装包
 tar -zxf gcc-8.3.0.tar.gz
#创建gcc安装路径
 mkdir -p /usr/local/gcc-8.3.0
#配置安装文件
 cd gcc-8.3.0
#后面的--with-gmp, --with-mpfr and/or --with-mpc选项一定要是相应安装的路径,否则会报出和上边同样的错误
 ./configure --prefix=/usr/local/gcc-8.3.0 --disable-multilib --with-gmp=/usr/local/gmp-6.1.2 --with-mpfr=/usr/local/mpfr-4.0.2 --with-mpc=/usr/local/mpc-1.1.0
#编译
 make
#make完成后,make install
 make install
 ln -s /usr/local/gcc-8.3.0 /usr/local/gcc
# 配置环境变量
 export PATH=/usr/local/gcc/bin:$PATH
 export LD_LIBRARY_PATH=/usr/local/gcc/lib64:/usr/local/gmp/lib:/usr/local/mpfr/lib:/usr/local/mpc/lib:$LD_LIBRARY_PATH
 export MANPATH=/usr/local/gcc/share/man:$MANPATH
# 查看gcc版本,判断gcc是否安装成功
 gcc --version

如果在安装gcc的make过程中报错“error: C++ preprocessor “/lib/cpp” fails sanity check”,是因为缺少C++必要的库,执行安装apt-get install build-essential。然后make clean后再make即可。

make 过程需要3个小时左右,make install过程需要一个小时左右,安心等待即可

    原文作者:tri_wheel
    原文地址: https://segmentfault.com/a/1190000019695955
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞