Linux平台下gcc安装升级

一、下载gcc源码

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

二、配置

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

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

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

mkdir build
cd build
../configure --prefix=/usr/local/gcc-5.5.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

三、编译

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

make -j$(nproc)

编译过程如果出现如下错误:

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

四、安装

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

sudo make install

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

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

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

gcc --version
g++ --version

相关推荐

  1. Linux平台gcc安装升级

    2024-01-06 13:16:01       45 阅读
  2. oracle linux 8升级gcc gcc9

    2024-01-06 13:16:01       40 阅读
  3. GCC 安装编译linux

    2024-01-06 13:16:01       38 阅读
  4. Linux平台安全编译

    2024-01-06 13:16:01       34 阅读
  5. 20.Ubuntu安装GCC

    2024-01-06 13:16:01       18 阅读
  6. Linux——gcc

    2024-01-06 13:16:01       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-06 13:16:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-06 13:16:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-06 13:16:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-06 13:16:01       20 阅读

热门阅读

  1. Kendo UI for Angular 学习笔记

    2024-01-06 13:16:01       42 阅读
  2. Redhat(liunx)连接虚拟机

    2024-01-06 13:16:01       25 阅读
  3. 阿里的通义灵码在android studio上的使用方法

    2024-01-06 13:16:01       53 阅读
  4. 07GoF之工厂模式

    2024-01-06 13:16:01       27 阅读
  5. K8S三种发布方式和声明式资源管理

    2024-01-06 13:16:01       32 阅读
  6. [原创][R语言]股票分析实战[8]:因子与subset的关系

    2024-01-06 13:16:01       31 阅读
  7. iOS基础之修饰符

    2024-01-06 13:16:01       41 阅读