多版本GCC安装及切换

目录

  • [1 背景](#1 背景)
  • [2 安装](#2 安装)
    • [2.1 Ubuntu 20.04](#2.1 Ubuntu 20.04)
    • [2.2 Ubuntu 18.04](#2.2 Ubuntu 18.04)
  • [3 配置](#3 配置)
  • [4 切换](#4 切换)
    • [4.1 切换到版本9](#4.1 切换到版本9)
    • [4.2 切换到版本10](#4.2 切换到版本10)

1 背景

最近在研究C++20中的协程需要安装GCC版本10。用到GCC多版本切换,记录步骤。

2 安装

2.1 Ubuntu 20.04

运行如下命令安装两个版本编译器:

bash 复制代码
sudo apt install gcc-9 g++-9
sudo apt install gcc-10 g++-10

2.2 Ubuntu 18.04

默认安装最高版本gcc-7,添加Toolchain Test Builds PPA(个人软件包存档),它提供了GCC 10的最新版本

bash 复制代码
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt install gcc-9 g++-9
sudo apt install gcc-10 g++-10

3 配置

使用update-alternatives命令配置多编译器版本:

bash 复制代码
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10

说明:

  • gcc与g++版本绑定在一起了,修改gcc版本会同时修改g++版本

4 切换

使用如下命令切换编译器版本:

bash 复制代码
sudo update-alternatives --config gcc

4.1 切换到版本9

bash 复制代码
$ 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-10   100       auto mode
  1            /usr/bin/gcc-10   100       manual mode
  2            /usr/bin/gcc-9    90        manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

查看版本:

bash 复制代码
$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.3) 9.4.0
Copyright (C) 2019 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.
$ g++ --version
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.3) 9.4.0
Copyright (C) 2019 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.

4.2 切换到版本10

bash 复制代码
$ 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-10   100       auto mode
  1            /usr/bin/gcc-10   100       manual mode
  2            /usr/bin/gcc-9    90        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1

查看版本:

bash 复制代码
$ gcc --version
gcc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
Copyright (C) 2020 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.
$ g++ --version
g++ (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
Copyright (C) 2020 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.
相关推荐
胡乱儿起个名8 天前
Relay算子注册(在pytorch.py端调用)
c++·人工智能·tvm·编译器·ai编译器
胡乱儿起个名13 天前
PrimExpr 与 RelayExpr 的区别
人工智能·tvm·编译器·ai编译器
云空14 天前
《Linux macOS :GCC升级方法》
linux·运维·macos·gcc
浅安的邂逅16 天前
Linux进程7-signal信号处理方式验证、可重入函数举例、信号集函数验证、信号集阻塞验证
linux·c语言·vim·进程通信·gcc
胡乱儿起个名1 个月前
Relay IR的核心数据结构
人工智能·tvm·编译器·ai编译器
航哥1 个月前
Go语言编译器的正确打开方式(二)- 通过Debug理解Go的编译过程
go·编译器
航哥1 个月前
Go语言编译器的正确打开方式(一)- 从源码编译 go
go·编译器
浅安的邂逅1 个月前
Linux Makefile-概述、语句格式、编写规则、多文件编程、Makefile变量分类:自定义变量、预定义变量
linux·c语言·vim·makefile·gcc
做人求其滴1 个月前
蓝桥杯C/C++省赛/国赛注意事项及运行环境配置
算法·蓝桥杯·编译器·c/c++·算法竞赛·运行环境·第十六届
重生之我在写代码2 个月前
如何进行apk反编译
android·程序员·编译器