centos7下默认gcc版本是4.8.5
这里yum仓库的镜像源选择的是阿里云:
具体怎么配置镜像源参考这篇:
CentOS7如何更换yum源和如何使用yum安装和卸载软件_centos7yum源-CSDN博客
1.安装开发工具(基本配置都有)
sudo yum groupinstall "Development Tools"
2.安装软件集合(Software Collections,简称 SCL):
Software Collections 允许你安装并使用比默认 CentOS 软件包更新的版本。安装 SCL 仓库:
sudo yum install centos-release-scl
执行完第二步以后 /etc/yum.repos.d 会多两个文件(如果没有就自己创建):
CentOS-SCLo-scl.repo
CentOS-SCLo-scl-rh.repo
这两个文件存放 devtoolset 以及 它的依赖文件 的镜像源,估计是阿里云镜像源已经停止更新的问题,CentOS-SCLo-scl.repo里面默认的镜像源 还是Centos官方 的古早网址(现在网址已经不一样了)
所以如果不更改 CentOS-SCLo-scl.repo 和 CentOS-SCLo-scl-rh.repo 内部设置的镜像源地址,就会一直报错。
更改如下:
# CentOS-SCLo-sclo.repo
#
# Please see http://wiki.centos.org/SpecialInterestGroup/SCLo for more
# information
[centos-sclo-sclo]
name=CentOS-7 - SCLo sclo
#这里7.6.1810对应的是Centos的版本,具体版本自行打开官网选择,最后保证路径对的上即可
baseurl=https://vault.centos.org/7.6.1810/sclo/x86_64/sclo/
#mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-sclo
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
#后面这些用不上
[centos-sclo-sclo-testing]
name=CentOS-7 - SCLo sclo Testing
baseurl=http://buildlogs.centos.org/centos/7/sclo/$basearch/sclo/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-sclo-source]
name=CentOS-7 - SCLo sclo Sources
baseurl=http://vault.centos.org/centos/7/sclo/Source/sclo/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-sclo-debuginfo]
name=CentOS-7 - SCLo sclo Debuginfo
baseurl=http://debuginfo.centos.org/centos/7/sclo/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
# CentOS-SCLo-rh.repo
#
# Please see http://wiki.centos.org/SpecialInterestGroup/SCLo for more
# information
[centos-sclo-rh]
name=CentOS-7 - SCLo rh
baseurl=https://vault.centos.org/7.6.1810/sclo/x86_64/rh/
#mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-rh
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
#后面这些用不上
[centos-sclo-rh-testing]
name=CentOS-7 - SCLo rh Testing
baseurl=http://buildlogs.centos.org/centos/7/sclo/$basearch/rh/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-rh-source]
name=CentOS-7 - SCLo rh Sources
baseurl=http://vault.centos.org/centos/7/sclo/Source/rh/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-rh-debuginfo]
name=CentOS-7 - SCLo rh Debuginfo
baseurl=http://debuginfo.centos.org/centos/7/sclo/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
3.安装新版本的 GCC:
安装你想要的 GCC 版本,例如 devtoolset-7:
sudo yum install devtoolset-7-gcc devtoolset-7-gcc-c++
4.启用新的 GCC 版本:(尝试性开启)
使用 scl 命令启用新版本的 GCC:
scl enable devtoolset-7 bash
5.验证新版本的 GCC:
验证新版本的 GCC 是否安装成功:
gcc -v
6.设置默认的 GCC 版本:( .bashrc 或者 .bash_profile)
如果你想要让新安装的 GCC 版本成为默认版本,需要更改环境变量。你可以将以下命令添加到你的 .bashrc 或者 .bash_profile文件中,以便每次打开终端时自动启用新版本的 GCC:
在 .bashrc 或者 .bash_profile里面添加:
source scl_source enable devtoolset-7
find / -name .bashrc #找.bashrc文件的位置
如果.bashrc中的设置可以生效,
但是.bash_profile中的设置不生效,
这通常是因为.bash_profile主要在登录时
(例如通过SSH登录到系统或在图形用户界面登录时) 被读取和执行,
而.bashrc则是在每次启动新的终端会话时被读取和执行。
这意味着,如果你是在图形用户界面下打开一个新的终端窗口或标签,
系统会读取.bashrc而不是.bash_profile。
为了确保无论是登录时还是打开新的终端时都能加载devtoolset-7,
你可以在.bashrc文件中添加启用devtoolset-7的命令。
如果你的.bash_profile没有生效,
可能是因为你没有以登录会话的方式启动终端(这在图形用户界面中是常见的)。