通过命令查询本机gcc/g++已安装的版本
ls /usr/bin/gcc*
ls /usr/bin/g++*
sudo gedit /etc/apt/sources.list
在文件尾部添加如下源,然后保存并更新
deb http://mirrors.aliyun.com/ubuntu/ xenial main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe
sudo apt-get update
安装gcc/g++ 5.4
sudo apt-cache policy gcc-5
安装其中一个版本
sudo apt-get install gcc-5=5.4.0-6ubuntu1~16.04.12
g++ 安装类似
sudo apt-cache policy g++-5
sudo apt-get install g++-5=5.4.0-6ubuntu1~16.04.12
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 40
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 50
sudo update-alternatives --config gcc
选择gcc-5对应的编号 回车即可
g++ 类似
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 40
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 50
sudo update-alternatives --config g++
选择g+±5对应的编号 回车即可
切换回高版本方法类似