在 Ubuntu上安装 多个版本的GCC 、 G++ 编译器

       以前在Ubuntu22上编译的时候用的gcc11,现在升级到ubuntu24,发现编译不行了,版本升级到了gcc13。只好开始找解决方案。

1、安装多个版本

sudo apt update
$ sudo apt install build-essential
$ sudo apt -y install gcc-11 g++-11 gcc-13 g++-13

2、使用 update-alternatives 工具创建多个 GCC 和 G++ 编译器替代方案的列表:
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 11
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 13

  3、检查 Ubuntu 系统上可用的 C 和 C++ 编译器列表,并通过输入相关的选择编号来选择所需的版本:

$ sudo update-alternatives --config gcc
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-9   9         auto mode
  1            /usr/bin/gcc-10  10         manual mode
* 2            /usr/bin/gcc-8   8         manual mode
  3            /usr/bin/gcc-9   9         manual mode
Press  to keep the current choice[*], or type selection number: 

对于 C++ 编译器执行:

$ sudo update-alternatives --config g++
There are 3 choices for the alternative g++ (providing /usr/bin/g++).

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/g++-9   9         auto mode
  1            /usr/bin/g++-10  10         manual mode
  2            /usr/bin/g++-8   8         manual mode
  3            /usr/bin/g++-9   9         manual mode

Press  to keep the current choice[*], or type selection number:

  4、每次切换后检查您当前选择的编译器版本:

$ gcc --version
$ g++ --version

这样就简单多了。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值