多版本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.
相关推荐
十五年专注C++开发2 天前
CMake基础:gcc/g++编译选项详解
开发语言·c++·gcc·g++
Jooolin2 天前
【编程史】IDE 是谁发明的?从 punch cards 到 VS Code
ai编程·visual studio code·编译器
huangyuchi.6 天前
【Linux】编译器gcc/g++及其库的详细介绍
linux·运维·服务器·笔记·编译器·gcc·g++
爱出名的狗腿子7 天前
vscode + cmake + ninja+ gcc 搭建MCU开发环境
ide·vscode·单片机·c·cmake·gcc·ninja
Mr_Chenph12 天前
VSCode + GD32F407 构建&烧录
vscode·单片机·gcc·gd32f407
Tipriest_15 天前
ubuntu20.04&vscode使用C++20(调整gcc版本&vscode设置)
ide·vscode·c++20·gcc
belldeep16 天前
C语言:gcc 或 g++ 数组边界检查方法
c语言·gcc·数组边界检查
Strugglingler16 天前
编译器ar命令参数
linux·ar·gcc
百年孤独百年21 天前
Conda 环境下安装 GCC 和 glibc (crypt.h) 教程
conda·glibc·gcc·图像恢复·低光照增强
咖喱年糕21 天前
【Linux】系统指令与开发全栈(vim、ssh、gcc)
linux·ssh·vim·gcc