https://askubuntu.com/questions/26498/choose-gcc-and-g-version

https://stackoverflow.com/questions/7832892/how-to-change-the-default-gcc-compiler-in-ubuntu

https://codeyarns.com/2015/02/26/how-to-switch-gcc-version-using-update-alternatives/

关于 update-alternatives 命令的帮助:         http://www.cnblogs.com/welhzh/p/7339632.html

First erased the current update-alternatives setup for gcc and g++:

sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++

Install Packages

It seems that both gcc-4.3 and gcc-4.4 are installed after install build-essential. However, we can explicitly install the following packages:

sudo apt-get install gcc-4.3 gcc-4.4 g++-4.3 g++-4.4

Install Alternatives

Symbolic links cc and c++ are installed by default. We will install symbol links for gcc and g++, then link cc and c++ to gcc and g++ respectively.

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.3 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 20 sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++

Configure Alternatives

The last step is configuring the default commands for gcc, g++. It's easy to switch between 4.3 and 4.4 interactively:

sudo update-alternatives --config gcc
sudo update-alternatives --config g++

Or switch using script:

#!/bin/sh

if [ -z "$1" ]; then
echo "usage: $0 version" 1>&2
exit 1
fi if [ ! -f "/usr/bin/gcc-$1" ] || [ ! -f "/usr/bin/g++-$1" ]; then
echo "no such version gcc/g++ installed" 1>&2
exit 1
fi update-alternatives --set gcc "/usr/bin/gcc-$1"
update-alternatives --set g++ "/usr/bin/g++-$1"

Here's a complete example of jHackTheRipper's answer for the TL;DR crowd. :-) In this case, I wanted to run g++-4.5 on an Ubuntu system that defaults to 4.6. As root:

apt-get install g++-4.5
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 100
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.5 50
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 100
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.5 50
update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.6 100
update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.5 50
update-alternatives --set g++ /usr/bin/g++-4.5
update-alternatives --set gcc /usr/bin/gcc-4.5
update-alternatives --set cpp-bin /usr/bin/cpp-4.5

Here, 4.6 is still the default (aka "auto mode"), but I explicitly switch to 4.5 temporarily (manual mode). To go back to 4.6:

update-alternatives --auto g++
update-alternatives --auto gcc
update-alternatives --auto cpp-bin

(Note the use of cpp-bin instead of just cpp. Ubuntu already has a cpp alternative with a master link of /lib/cpp. Renaming that link would remove the /lib/cpp link, which could break scripts.)

ubuntu 里切换 gcc,g++ 的版本的更多相关文章

  1. gcc/g++多版本切换 (ubuntu18.04)

    使用Ubuntu18.04已经有一段时间了,在使用过程中经常需要处理不同软件的编译工作,但是这时候就遇到这样一个问题,那就是不同软件,甚至是同一个软件的不同版本都会使用不同版本的gcc/g++来进行编 ...

  2. ubuntu 14.04 gcc/g++版本降低

    Matlab 2014a支持的gcc/g++版本是4.7x,而ubuntu 14.04以及更高版本默认安装的版本都是4.8及以上,因此需要降低gcc/g++版本,方法如下: 1 安装 $ sudo a ...

  3. Debian/Ubuntu 已安装gcc/g++ 4.8.1

    gcc 4.8.1 是第一个全然支持C++11(C++14非常可能在gcc 4.9.0開始支持.)的编译器,Windows上能够安装mingw版的.在sourceforge 上有下载.安装也比較方便. ...

  4. 通过yum升级gcc/g++至版本4.8.2

    最近在坐一个日期处理的问题,需要安装sxtwl模块,但是gcc版本4.7死活也安装不上,最后测试到只能升级gcc版本到4.8才得以解决 [root@123 bin]# gcc -vUsing buil ...

  5. ubuntu下切换默认的python版本

    Ubuntu系统自带python2.7(默认)和python3.5,所以不需要自己安装python.我们可以使用命令python -V或python3 -V来查看默认的python版本. 现在越来越多 ...

  6. ubuntu 18.04 gcc g++降级4.8版

    $ sudo apt-get install -y gcc-4.8 $ sudo apt-get install -y g++-4.8 $ cd /usr/bin $ sudo rm gcc $ su ...

  7. ubuntu 14.04 更新 gcc/g++ 4.9.2

    ubuntu 14.04 更新 gcc/g++ 4.9.2 最近看到c++11非常的好用,尤其是自带了regex,于是稍微学了一下c++11的新特性.可是我在编译一个regex程序是却发现稍微复杂一点 ...

  8. ubuntu 16.04 更新 gcc/g++ 4.9.2

    ubuntu 转载 2016年10月12日 :: 标签:ubuntu /g++ /gcc [html] view plain copy sudo dpkg -l g++ 最近在学C++primer , ...

  9. ubuntu下安装低级版本gcc/g++ 并随意切换

    来自:http://blog.sina.com.cn/s/blog_6cee149d010129bl.html 发现Android的版本中编译Host的程序经常因为本机的Gcc版本过高,需要这样那样的 ...

随机推荐

  1. 自学Zabbix13.2 分布式监控proxy配置

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix13.2 分布式监控proxy配置 分为两部分: 安装proxy 配置proxy ...

  2. 【BZOJ4259】残缺的字符串(FFT)

    [BZOJ4259]残缺的字符串(FFT) 题面 给定两个字符串\(|S|,|T|\),两个字符串中都带有通配符. 回答\(T\)在\(S\)中出现的次数. \(|T|,|S|<=300000\ ...

  3. 沉迷Link-Cut tree无法自拔之:[BZOJ2049]洞穴勘探(蒟蒻的LCT板子)

    来自蒟蒻 \(Hero \_of \_Someone\) 的 \(LCT\) 学习笔记 最近学了一波 \(LCT\) , 于是怒刷 \(LCT\) 合集...... $ $ 学的时候借鉴了 Clove ...

  4. SP1805 HISTOGRA (单调栈)

    单调栈维护栈顶为高度最大的 记下来栈中每个元素入栈时顶掉的最靠左的一个位置(如果没顶掉就是它本身),那么在它出栈的时候,它所带来的面积就是(出栈位置-记录位置)*高度 (可能会有加一减一之类的细节) ...

  5. luogu1600 [NOIp2016]天天爱跑步 (tarjanLca+dfs)

    经过部分分的提示,我们可以把一条路径切成s到lca 和lca到t的链 这样就分为向上的链和向下的链,我们分开考虑: 向上:如果某一个链i可以对点x产生贡献,那么有deep[x]+w[x]=deep[S ...

  6. hdu 2577 How to Type(dp)

    Problem Description Pirates have finished developing the typing software. He called Cathy to test hi ...

  7. Linux下Vim编辑器访问系统剪切板

    默认情况下VIM使用的是内置的寄存器,而不是X Window的剪切板 启用系统剪切板支持 可以使用vim --version | grep clipboard查看,若clipboard前显示-标记,而 ...

  8. git 合并连续的几个 commits

    命令 git rebase -i HEAD~[N],如 git rebase -i HEAD~3 合并最近 3 个 commit. 运行上述界面后会进入一个编辑界面,快捷键是 vim 的快捷键. 修改 ...

  9. banner轮播无缝滚动 jq代码

    HTML: <div class="box"> <ul> <li>11111</li> <li>22222</li ...

  10. P5002 专心OI - 找祖先

    P5002 专心OI - 找祖先 给定一棵有根树(\(n \leq 10000\)),\(M \leq 50000\) 次询问, 求以 \(x\) 为 \(LCA\) 的点对个数 错误日志: 看下面 ...