2025-07-04【Linux】Ubuntu18.04.6升级gcc/g++11

Ubuntu18.04.6默认gcc版本如下:

gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 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.

需要安装支持c++17特性的g++11

安装

依次输入以下指令:

 sudo apt-get update 
 sudo apt-get install build-essential software-properties-common -y 
 sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 
 sudo apt-get update 
 sudo apt-get install gcc-snapshot -y 
 sudo apt-get update 
 sudo apt-get install gcc-11 g++-11 -y 

版本控制

使用指令ls /usr/bin/g++*查看系统的g++数量及版本

root@ubuntu:~# ls /usr/bin/g++*
/usr/bin/g++  /usr/bin/g++-11  /usr/bin/g++-7
root@ubuntu:~# 

为每个版本配置替代版本,并将优先级与之关联,系统默认版本为优先级最高的版本。
最后的数字为优先级,值越大优先级越高

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 90
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 90

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100

目前检查版本即为优先级最高版本:

root@ubuntu:~# gcc --version
gcc (Ubuntu 11.4.0-2ubuntu1~18.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.

切换版本
使用指令sudo update-alternatives --config gcc
出现以下提示

root@ubuntu:~# sudo update-alternatives --config gcc
有 2 个候选项可用于替换 gcc (提供 /usr/bin/gcc)。

  选择       路径           优先级  状态
------------------------------------------------------------
* 0            /usr/bin/gcc-11   100       自动模式
  1            /usr/bin/gcc-11   100       手动模式
  2            /usr/bin/gcc-7    90        手动模式

要维持当前值[*]请按<回车键>,或者键入选择的编号:

输入对应编号再按回车即可重新选择版本

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
none
暂无评论...