多版本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.
相关推荐
Ciderw1 天前
LLVM编译器简介
c++·golang·编译·编译器·gcc·llvm·基础设施
瞌睡不来4 天前
(学习总结25)Linux工具:vim 编辑器 和 gcc/g++ 编译器
linux·学习·编辑器·vim·编译器·gcc/g++
却道天凉_好个秋6 天前
c++ gcc工具链
c++·gcc·交叉编译·编译优化
小志biubiu1 个月前
编译器gcc/g++ --【Linux基础开发工具】
linux·运维·服务器·c++·笔记·gcc·g++
Once_day1 个月前
GCC之编译(8)AR打包命令
ar·gnu·gcc
HyperAI超神经1 个月前
【TVM教程】为 ARM CPU 自动调优卷积网络
arm开发·人工智能·python·深度学习·机器学习·tvm·编译器
利刃大大1 个月前
【Linux入门】2w字详解yum、vim、gcc/g++、gdb、makefile以及进度条小程序
linux·c语言·vim·makefile·gdb·gcc
jiuri_12151 个月前
GD32F303 GCC 环境搭建
gd32·gcc
BIT祝威1 个月前
拥有自己的解析器(C#实现LALR(1)语法解析器和miniDFA词法分析器的生成器)
markdown·面向对象·编译器·compiler
一丝晨光1 个月前
GCC支持Objective C的故事?Objective-C?GCC只能编译C语言吗?Objective-C 1.0和2.0有什么区别?
c语言·开发语言·ios·objective-c·msvc·clang·gcc