添加本地source
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
安装 gcc9 和 g++9(ubuntu18.04)
sudo apt-get install gcc-9 gcc-9-multilib g++-9 g++-9-multilib
安装 gcc13 和 g++13(ubuntu22.04)
sudo apt install gcc-13 -y
sudo apt install g++-13 -y
sudo apt install gcc-13-multilib -y
sudo apt install g++-13-multilib -y
添加和修改默认配置
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 80
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 80
注:最后那个数字代表优先级,默认选优先级高的,数字越小优先级越高。
sudo update-alternatives --config gcc
出现下列类似信息
选择 路径 优先级 状态
------------------------------------------------------------
* 0 /usr/bin/gcc-9 90 自动模式
1 /usr/bin/gcc-8 50 手动模式
2 /usr/bin/gcc-9 90 手动模式
3 /usr/bin/gcc-7 30 手动模式
要维持当前值[*]
请按回车键,或者键入选择的编号:
输入需要作为默认编辑器的gcc版本的编号 1-3
gcc
sudo update-alternatives --config gcc
g++
sudo update-alternatives --config g++
删除已有配置,如果想删除可选项的话可以键入以下指令:
sudo update-alternatives --remove gcc /usr/bin/gcc-9
当前版本确认
gcc --version
g++ --version
参考:
- https://blog.csdn.net/qq_39779233/article/details/105124478
- https://blog.csdn.net/u013187057/article/details/84996127
- https://tuxamito.com/wiki/index.php/Installing_newer_GCC_versions_in_Ubuntu
➜ /home/mi/local/n62-4.0 sudo update-alternatives --config gcc
There are 4 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/gcc-9 50 auto mode
1 /usr/bin/gcc-10 45 manual mode
2 /usr/bin/gcc-11 40 manual mode
* 3 /usr/bin/gcc-13 35 manual mode
4 /usr/bin/gcc-9 50 manual mode
Press <enter> to keep the current choice[*], or type selection number:
➜ /home/mi/local/n62-4.0 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc 30
➜ /home/mi/local/n62-4.0 sudo update-alternatives --config gcc
➜ /home/mi/local/n62-4.0 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 30 --slave /usr/bin/g++ g++ /usr/bin/g++-11
➜ /home/mi/local/n62-4.0 sudo update-alternatives --config gcc
There are 4 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/gcc-9 50 auto mode
1 /usr/bin/gcc-10 45 manual mode
2 /usr/bin/gcc-11 30 manual mode
* 3 /usr/bin/gcc-13 35 manual mode
4 /usr/bin/gcc-9 50 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/gcc-11 to provide /usr/bin/gcc (gcc) in manual mode
➜ /home/mi/local/n62-4.0 gcc --version
gcc (Ubuntu 11.4.0-2ubuntu1~20.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
➜ /home/mi/local/n62-4.0 g++ --version
g++ (Ubuntu 11.4.0-2ubuntu1~20.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
➜ /home/mi/local/n62-4.0 sudo update-alternatives --config gcc
There are 4 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/gcc-9 50 auto mode
1 /usr/bin/gcc-10 45 manual mode
* 2 /usr/bin/gcc-11 30 manual mode
3 /usr/bin/gcc-13 35 manual mode
4 /usr/bin/gcc-9 50 manual mode
Press <enter> to keep the current choice[*], or type selection number: ^C
➜ /home/mi/local/n62-4.0