CentOS安装GCC-8.3.0及其依赖

安装GCC8.3.0,这个版本已经能够支持C++11、C++14、C++17,还不支持C++20,感谢这位博主

GCC的依赖顺序及版本表

名称 版本 国内镜像地址
m4 m4-1.4.16 下载地址
gmp gmp-6.1.2 下载地址
mpfr mpfr-4.0.2 下载地址
mpc mpc-1.1.0 下载地址
GCC GCC-8.3.0 下载地址

严格按照下列安装顺序

安装m4


下载 m4-1.4.16.tar.gz后

tar xzf m4-1.4.16.tar.gz
cd m4-1.4.16
./configure --prefix=/usr/local/m4-1.4.16
make
make install
ln -s /usr/local/m4-1.4.16 /usr/local/m4

可能遇见的错误:

  • stdio.h中gets错误

    编译m4时可能会遇见如下错误:

      ./stdio.h:477:1: error: ‘gets’ undeclared here (not in a function)
      _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
    

    此时用vi打开stdio.h文件,找到这段代码:

      /* It is very rare that the developer ever has full control of stdin,
         so any use of gets warrants an unconditional warning.  Assume it is
         always declared, since it is required by C89. */
      #undef gets
      _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
    

    修改后:

      /* It is very rare that the developer ever has full control of stdin,
         so any use of gets warrants an unconditional warning.  Assume it is
         always declared, since it is required by C89. 
      #undef gets
      _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");*/
    	
      #if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(2, 16)
      _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
      #endif
    

设置环境变量:

vi /etc/profile

// 文件尾追加
PATH=/usr/local/m4/bin:$PATH
export PATH

保存后,应使修改生效:

source /etc/profile

可以通过echo $PATH命令查看是否添加成功

安装gmp


下载 gmp-6.1.2.tar.xz后

xz -d gmp-6.1.2.tar.xz
tar xf gmp-6.1.2.tar
cd gmp-6.1.2
./configure --prefix=/usr/local/gmp-6.1.2
make
make install
ln -s /usr/local/gmp-6.1.2 /usr/local/gmp

安装mpfr


下载 mpfr-4.0.2.tar.gz后

tar xzf  mpfr-4.0.2.tar.gz
cd mpfr-4.0.2
./configure --prefix=/usr/local/mpfr-4.0.2 --with-gmp=/usr/local/gmp
make
make install
ln -s /usr/local/mpfr-4.0.2 /usr/local/mpfr

安装mpc


下载 mpc-1.1.0.tar.gz后

tar xzf  mpc-1.1.0.tar.gz
cd mpc-1.1.0
./configure --prefix=/usr/local/mpc-1.1.0 --with-gmp=/usr/local/gmp --with-mpfr=/usr/local/mpfr
make
make install
ln -s /usr/local/mpc-1.1.0 /usr/local/mpc

设置LD_LIBRARY_PATH环境变量


vi /etc/profile

// 文件尾追加
LD_LIBRARY_PATH=/usr/local/gmp/lib:/usr/local/mpfr/lib:/usr/local/mpc/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

保存后,应使修改生效:

source /etc/profile

安装GCC


下载 gcc-8.3.0.tar.gz后

tar xzf gcc-8.3.0.tar.gz
cd gcc-8.3.0
./configure --prefix=/usr/local/gcc-8.3.0 --with-mpfr=/usr/local/mpfr --with-gmp=/usr/local/gmp --with-mpc=/usr/local/mpc
make
make install
ln -s /usr/local/gcc-8.3.0 /usr/local/gcc

可能遇见的错误:

  • 不支持32位程序

    如果系统不支持32位程序,则需要增加–disable-multilib

      ./configure --prefix=/usr/local/gcc-8.3.0 --with-mpfr=/usr/local/mpfr --with-gmp=/usr/local/gmp --with-mpc=/usr/local/mpc --disable-multilib
    

    同时有可能遇见内存不足的错误internal compiler error,只能换更大内存的机器这个编译需要很长时间,我编译时是在晚上睡觉前执行编译,第二天早晨完全可以编译好

  • C++ preprocessor “/lib/cpp” fails sanity check错误

    CentOS系统缺少必要的C++库,只需安装C++库:

      yum install glibc-headers
    	
      yum install gcc-c++ 
    

设置环境变量:

vi /etc/profile
	
// 文件尾追加
PATH=/usr/local/gcc/bin:$PATH
CC=/usr/local/gcc/bin/gcc
CXX=/usr/local/gcc/bin/g++
MANPATH=/usr/local/gcc/share/man:$MANPATH
LD_LIBRARY_PATH=/usr/local/gcc/lib64:$LD_LIBRARY_PATH

export PATH
export LD_LIBRARY_PATH
export CC
export CXX
export MANPATH

保存后,应使修改生效:

source /etc/profile

可以通过echo $PATH命令查看是否添加成功,到此已经安装完成,可以看看我们的安装结果:

gcc --version