Linux平台下gcc安装升级

文章目录

  • 一、下载gcc源码
  • 二、配置
  • 三、编译
  • 四、安装

一、下载gcc源码

到gcc官网下载所要安装的版本,例如:gcc-7.3.0

二、配置

首先确保系统已经安装了必要的依赖项。在命令行中运行以下命令来更新包管理器并安装所需的构建工具:

sudo apt update && sudo apt upgrade -y build-essential

解压gcc源码压缩包,例如:gcc-7.3.0.tar.gz。进入gcc-7.3.0目录。执行(注意:根据系统和需求调整选项):

mkdir build
cd build
../configure --prefix=/usr/local/gcc-7.3.0 --enable-languages=c,c++,fortran --disable-multilib

编译过程中可能会出现如下错误:

configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations. Source code for these libraries can be found at
their respective hosting sites as well as at
ftp://gcc.gnu.org/pub/gcc/infrastructure/. See also
http://gcc.gnu.org/install/prerequisites.html for additional info. If
you obtained GMP, MPFR and/or MPC from a vendor distribution package,
make sure that you have installed both the libraries and the header
files. They may be located in separate packages.

错误信息中说明,安装gcc需要这三个依赖:GMP 4.2+MPFR 2.4.0+MPC 0.8.0+。解决办法:

  • 安装GMP
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2
tar -jxvf gmp-6.1.0.tar.bz2
cd gmp-6.1.0
./configure
make && make install

可能遇到错误:
checking for suitable m4... configure: error: No usable m4 in $PATH or /usr/5bin (see config.log for reasons).
则需要安装m4sudo apt-get install m4

  • 安装MPFR
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2
tar -jxvf mpfr-3.1.4.tar.bz2
cd mpfr-3.1.4
./configure
make && make install
  • 安装MPC
wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz
tar -zxvf mpc-1.0.3.tar.gz
cd mpc-1.0.3
./configure
make && make install

如果遇到报错:

mul.c:175:1: error: conflicting types for ‘mpfr_fmma’
  175 | mpfr_fmma (mpfr_ptr z, mpfr_srcptr a, mpfr_srcptr b, mpfr_srcptr c,
      | ^~~~~~~~~
In file included from mpc.h:25,
                 from mpc-impl.h:30,
                 from mul.c:22:

则将mpc/src/mul.c中的mpfr_fmma函数改名为mpfr_fmma_mul,一共三处。

三、编译

完成配置后,运行make命令开始编译GCC:

make -j$(nproc)

可能出现的错误

  • 错误1
checking LIBRARY_PATH variable... contains current directory
configure: error: 
*** LIBRARY_PATH shouldn't contain the current directory when
*** building glibc. Please change the environment variable
*** and run configure again.

出现这个错误的原因是由于环境变量的LIBRARY_PATH中出现了当前目录,这对gcc编译来说是多余的。解决方法:unset LD_LIBRARY_PATH

  • 错误2
    Linux平台下gcc安装升级_第1张图片
    解决方法:编辑gcc源文件/libsanitizer/sanitizer-common/sanitizer-platform-limits-posix.cc,查找ustat,把其所在行注释掉。

四、安装

当编译完成时,运行以下命令以安装新版本的GCC:

sudo make install

若要将新版本的GCC设置为默认版本,可以运行以下命令:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/local/gcc-7.3.0/bin/gcc 100
sudo update-alternatives --set gcc /usr/local/gcc-7.3.0/bin/gcc
sudo update-alternatives --install /usr/bin/g++ g++ /usr/local/gcc-7.3.0/bin/g++ 100
sudo update-alternatives --set g++ /usr/local/gcc-7.3.0/bin/g++

最后,可以通过运行以下命令来验证GCC版本是否正确:

gcc --version
g++ --version

你可能感兴趣的:(Linux,linux,运维,服务器)