Ubuntu20.04切换不同版本的g++
Ubuntu自带的gcc和g++的版本为9.4,但是所需要运行的项目需要的依赖为4.9,因此需要安装不同版本的gcc和g++,并且提供一个方便管理和切换g++版本的方法。
修改sources.list文件
- ubuntu中直接安装4.9会出现报错,我们需要现在界面Toolchain test builds查看gcc-4.9所对应的ubuntu的版本
可以看到gcc-4.9对应版本在ubuntu1-16.04(Xenial Ubuntu16.04)和ubuntu1~14.04.1(Trusty Ubuntu 14.04)中。
使用命令lsb_release -a
可以看到输出本机的codename为:Codename: focal
- 下面需要在source中加入下载源:
cd /etc/apt or
sudo vim /etc/apt/sources.list
- 在文件后面添加:
deb http://dk.archive.ubuntu.com/ubuntu/ xenial main
deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe
如下图所示:
3. 更新并且安装g++和gcc
sudo apt update
sudo apt-get install gcc-4.9 g++-4.9
参考链接:
https://stackoverflow.com/questions/68805002/i-need-to-install-gcc4-9-on-ubuntu-20-04-matlab-mex
https://www.cnblogs.com/zzoo/p/ubuntu20_04_3-gcc_4_8_5.html
管理多个g++和gcc
- 安装之后,已经可以看到所需版本的文件夹
- 下面使用
update-alternatives
来管理多个 g++ 版本:
- 添加不同版本的g++和gcc以及优先级
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 10
- 切换g++的版本
sudo update-alternatives --config g++
-
选择所需版本的编号
-
检查是否成功
g++ --version
gcc --version