ubuntu 更新或更改GCC/G++

最近遇到一些问题,需要用到gcc-9/g++-9,但是我自带的ubuntu18.04是gcc-7.5/g++-7.5,所以升级一下,奈何文章太多而且很多无效,所以在此记录一下:

参考:https://stackoverflow.com/questions/19836858/upgrade-gcc-4-6-3-on-ubuntu-12-04-to-4-8-2

update-alternatives 是一个用于管理 Linux 系统中多个软件版本之间的符号链接的工具。它允许用户在多个安装的软件版本之间进行切换,使得系统可以轻松地在不同版本之间切换而无需手动更改符号链接。

bash 复制代码
# 1. Add the ppa by
sudo add-apt-repository ppa:ubuntu-toolchain-r/test

# 2. Install g++ and gcc (version 4.8),我安装的9,版本根据你实际的来
# sudo apt-get update; sudo apt-get install gcc-4.8 g++-4.8
sudo apt-get update; sudo apt-get install gcc-9 g++-9

# 3. Run the following commands one by one,
# 移除掉原有的所有管理器管理的版本, 没有可以跳过
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++
# 设置新的版本管理
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 20
# 更新管理器
sudo update-alternatives --config gcc
sudo update-alternatives --config g++

gcc -v # 9.0输出
g++ -v # 9.0输出

如果你在第一步遇到:

bash 复制代码
aptsources.distro.NoDistroTemplateException: Error: could not find a distribution template for Kylin/kylin

说明你可能是双系统或者多系统或者自己安装了什么软件把系统发行版本改了,需要修改回来:

bash 复制代码
sudo gedit /etc/lsb-release


# 粘贴到你的这个文件中去,具体的最后一行描述可以根据uname -a来看
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS
相关推荐
EndingCoder几秒前
类的继承和多态
linux·运维·前端·javascript·ubuntu·typescript
信创天地34 分钟前
深耕金融政务核心场景:国产化数据库迁移的全流程架构设计与风险管控
运维·网络安全·系统架构·系统安全·运维开发
ZFB00011 小时前
【麒麟桌面系统】V10-SP1 2503 系统知识——添加用户
linux·运维·kylin
释怀不想释怀1 小时前
Docker(网络)
运维·docker·容器
ZFB00011 小时前
【麒麟桌面系统】V10-SP1 2503 系统知识——进入救援模式
linux·运维·kylin
RisunJan1 小时前
Linux命令-iptables-restore命令(从文件批量恢复 iptables 防火墙规则)
linux·运维·网络
轻造科技2 小时前
周期压缩极限挑战:APS排产优化这样实现
运维·devops
鲨莎分不晴2 小时前
Nginx 部署前端项目实战指南
运维·前端·nginx
hugerat3 小时前
在AI的帮助下,用C++构造微型http server
linux·c++·人工智能·http·嵌入式·嵌入式linux
ha20428941943 小时前
Linux操作系统学习记录之----自定义协议(网络计算器)
linux·网络·学习