Ubuntu多版本(低版本)gcc/g++安装、切换与卸载图文教程
创始人
2024-11-30 15:06:25
0

目录

  • 1 问题背景
  • 2 多版本安装
  • 3 多版本切换
  • 4 多版本卸载
  • 5 其他问题

1 问题背景

环境:

  • gcc 9.4.0
  • g++ 9.4.0
  • Ubuntu20.04

现象:通过apt install build-essential安装的gccg++默认是当前版本系统支持的最高版本编译器,但是很多工程的编译需要安装低版本

2 多版本安装

终端输入

apt-cache policy gcc-9 

在这里插入图片描述
可以发现有不少gcc-9的发行版可供下载。假设我们需要安装gcc-5,输入

apt-cache policy gcc-5 

发现

在这里插入图片描述
所以此时直接安装会显示E: Package 'gcc-5' has no installation candidate。本质原因在于,Ubuntu 20.04使用的软件源只包含gcc-9而没有gcc-5gcc-5存在于更低版本的Ubuntu源中。那么解决方案就是添加低版本的Ubuntu源,具体地

sudo vim /etc/apt/sources.list 

在末尾添加

deb http://mirrors.aliyun.com/ubuntu/ xenial main deb-src http://mirrors.aliyun.com/ubuntu/ xenial main deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main deb http://mirrors.aliyun.com/ubuntu/ xenial universe deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb http://mirrors.aliyun.com/ubuntu/ xenial-security main deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe 

上面源中每一行中的xenial标识为ubuntu16.04,若为其他版本Ubuntu,将对应xenial改为其他版本标识,常用的Ubuntu版本代号如下:

  • Ubuntu 22.04jammygcc-11
  • Ubuntu 20.04focalgcc-9
  • Ubuntu 18.04bionicgcc-7
  • Ubuntu 16.04xenialgcc-5

接着更新软件缓存并再次查找gcc-5缓存

sudo apt-get update apt-cache policy gcc-5 

在这里插入图片描述
选择其中一个版本进行安装即可

sudo apt-get install gcc-5=5.4.0-6ubuntu1~16.04.12 sudo apt-get install g++-5=5.4.0-6ubuntu1~16.04.12 

3 多版本切换

通过设置不同版本的优先级实现切换

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 40 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 50 sudo update-alternatives --config gcc 

在这里插入图片描述

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 40 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 50 sudo update-alternatives --config g++ 

此时再输入

gcc --version 

实现了从gcc-9gcc-5的切换

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609 Copyright (C) 2015 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 多版本卸载

终端输入

sudo apt purge --autoremove -y gcc-5 g++-5 

即可。其他版本同理

5 其他问题

  1. 公钥缺失
    W: GPG error: http://mirrors.aliyun.com/ubuntu xenial InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 40976EAF437D05B5 NO_PUBKEY 3B4FE6ACC0B21F32 E: The repository 'http://mirrors.aliyun.com/ubuntu xenial InRelease' is not signed. N: Updating from such a repository can't be done securely, and is therefore disabled by default. N: See apt-secure(8) manpage for repository creation and user configuration details. 
    解决方案:根据报错信息NO_PUBKEY 40976EAF437D05B5 NO_PUBKEY 3B4FE6ACC0B21F32导入对应公钥即可
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5 sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32 

🔥 更多精彩专栏

  • 《ROS从入门到精通》
  • 《Pytorch深度学习实战》
  • 《机器学习强基计划》
  • 《运动规划实战精讲》

👇源码获取 · 技术交流 · 抱团学习 · 咨询分享 请联系👇

相关内容

热门资讯

透视app!wejoker辅助... 透视app!wejoker辅助软件,土豪联盟辅助器,好像真的有挂(科普wepoke教程)-哔哩哔哩1...
透视教学!约局吧辅助脚本,aa... 透视教学!约局吧辅助脚本,aapoker破解侠是真的吗,模块教程(其实有挂)-哔哩哔哩;1)aapo...
透视控制!wepoker辅助器... 透视控制!wepoker辅助器激活码(透视)开挂辅助app(果然真的有挂)-哔哩哔哩1、透视控制!w...
有消息称!福建天庭开心辅助软件... 您好,福建天庭开心辅助软件这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
透视了解!hhpoker辅助实... 透视了解!hhpoker辅助实战视频,佛手大菠萝13道挂哪里,攻略教程(竟然存在有挂)-哔哩哔哩运佛...
此事迅速冲上热搜!hhpoke... 此事迅速冲上热搜!hhpoker德州透视挂,丫丫衡阳字牌3辅助,确实有挂(总结科技教程)-哔哩哔哩1...
透视测试!wepoker免费透... 透视测试!wepoker免费透视脚本(透视)开挂辅助器(好像真的是有挂)-哔哩哔哩1、玩家可以在we...
透视辅助!epoker透视底牌... 透视辅助!epoker透视底牌,wepoker线上大神,指南教程(其实是有挂)-哔哩哔哩进入游戏-大...
有玩家发现!潮友会app下载安... 有玩家发现!潮友会app下载安卓,新九哥智能辅助app,透明挂教程(其实存在有挂)-哔哩哔哩1、新九...
透视规律!哈糖大菠萝有没有挂(... 透视规律!哈糖大菠萝有没有挂(透视)开挂辅助软件(真是是有挂)-哔哩哔哩1、许多玩家不知道哈糖大菠萝...