1. 安装新版本
sudo apt install gcc-12 g++-12
2. 查看所有gcc版本
sudo update-alternatives --list gcc
输出:
/usr/bin/gcc-11
/usr/bin/gcc-12
3. 设置各个版本优先级
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11 --slave /usr/bin/g++ g++ /usr/bin/g++-11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 12 --slave /usr/bin/g++ g++ /usr/bin/g++-12
4、选择默认版本
sudo update-alternatives --config gcc
输出:
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/gcc-12 12 auto mode
1 /usr/bin/gcc-11 11 manual mode
2 /usr/bin/gcc-12 12 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
选择对应的序号即可