当前位置:   article > 正文

Gcc多版本安装和切换_gcc 切换版本

gcc 切换版本

添加本地source

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
  • 1
  • 2

安装 gcc9 和 g++9(ubuntu18.04)

sudo apt-get install gcc-9 gcc-9-multilib g++-9 g++-9-multilib
  • 1

安装 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
  • 1
  • 2
  • 3
  • 4

添加和修改默认配置

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
  • 1
  • 2

注:最后那个数字代表优先级,默认选优先级高的,数字越小优先级越高

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        手动模式
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

输入需要作为默认编辑器的gcc版本的编号 1-3
gcc

sudo update-alternatives --config gcc
  • 1

g++

sudo update-alternatives --config g++
  • 1

删除已有配置,如果想删除可选项的话可以键入以下指令:

sudo update-alternatives --remove gcc /usr/bin/gcc-9
  • 1

当前版本确认

gcc --version
g++ --version
  • 1
  • 2

参考:


➜  /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 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/260880
推荐阅读
相关标签
  

闽ICP备14008679号