CentOS7中升级gcc

先情提要

Centos 7 默认 gcc 版本为 4.8,根本满足不了实际需求,使用 yum 也只会更新到维护期 15 年版本

实施过程

1、安装 centos-release-scl

sudo yum install centos-release-scl-rh

2、安装 devtoolset,注意,如果想安装 7.版本的,就改成 devtoolset-7-gcc,以此类推,这里安装 Version 9

sudo yum install devtoolset-9*

3、激活对应的 devtoolset

scl enable devtoolset-9 bash

4、注册 devtoolset

进入/opt/rh 目录,source ./devtoolset-9/enable

记得注册成开机启动(/etc/rc.d/rc.local)

source /opt/rh/devtoolset-9/enable

5、替换旧版 gcc

1
2
3
4
5
6
mv /usr/bin/gcc /usr/bin/gcc-4.8.5
ln -s /opt/rh/devtoolset-9/root/bin/gcc /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++-4.8.5
ln -s /opt/rh/devtoolset-9/root/bin/g++ /usr/bin/g++
gcc --version
g++ --version

6、二次确认安装是否就绪

查看当前 gcc

which gcc

/opt/rh/devtoolset-9/root/usr/bin/gcc

在 yum 查看 gcc 资讯

yum info devtoolset-9-gcc-c++

Installed Packages

Name : devtoolset-9-gcc-c++

Arch : x86_64

Version : 9.3.1

Release : 2.2.el7

Size : 31 M

Repo : installed

From repo : centos-sclo-rh

Summary : C++ support for GCC version 9

URL : http://gcc.gnu.org

License : GPLv3+ and GPLv3+ with exceptions and GPLv2+ with exceptions and LGPLv2+ and BSD

Description : This package adds C++ support to the GNU Compiler Collection

        : version 9.  It includes support for most of the current C++ specification

        : and a lot of support for the upcoming C++ specification.

查看 gcc 版本

gcc -v

Thread model: posix

gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)

若无误,则升级成功。

作者

Catooilg

发布于

2023-10-14

更新于

2025-02-10

许可协议

评论