快速升级到g++11和gcc11

目的:在Ubuntu18.04上把g++7.5升级到g++-11

方法步骤:

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update && sudo apt install gcc-11 g++-11

上面三行代码就可以在一个啥都没有的ubuntu上安装g++11的环境啦。但是,如何对运行环境默认使用的g++的版本进行更换呢?

注意⚠️,推荐使用方法二。

方法一:

我尝试了update-alternatives命令,期待查看一下候选的gcc版本有哪些,理想的状况是下面这样的。但是…

$ sudo update-alternatives --config gcc
There are 6 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-11   1010      auto mode
  1            /usr/bin/gcc-10   1000      manual mode
  2            /usr/bin/gcc-11   1010      manual mode
  3            /usr/bin/gcc-5    40        manual mode
  4            /usr/bin/gcc-7    700       manual mode
  5            /usr/bin/gcc-8    800       manual mode
  6            /usr/bin/gcc-9    900       manual mode

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

但是我的情况是:

update-alternatives: error: no alternatives for gcc

我推测是,我刚刚安装的还没有告诉运行环境,所以它不知道有另外一个版本的存在。
怎么搞呢,就用下面这个办法。把g++-11绑定到g++这个命令上面,绑定到这个默认的link,/usr/bin/g++上面。后面的20代表的是优先级。

$  sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 20

此时再用

$ sudo update-alternatives --config g++

就可以查看到你的机器上的g++有哪些版本了,并且表中还会有不同版本的优先级,这个优先级数字越大的就越是被优先使用。

拓展一下,当你安装一个新版本的g++-11的时候,不止是g++-11被添加到了/usr/bin/这个目录下面。还有很多g++相关的工具。

因此要想实现,把这些附加工具统一的进行版本更换。可以用下面这个更简洁的命令。解释一下update-alternatives 命令的选项的含义。

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10 \
    --slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-4.3 \
    --slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-4.3 \
    --slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-4.3

方法二:

apt install命令完成之后,使用下面这一行的命令,完成新版本的g++11编译器的软连接。注意了,ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接。

ls -la /usr/bin/ | grep -oP "[\S]*(gcc|g\+\+)(-[a-z]+)*[\s]" | xargs bash -c 'for link in ${@:1}; do echo sudo ln -s -f "/usr/bin/${link}-${0}" "/usr/bin/${link}"; done' 4.9

下面对这个命令进行一个解释:

Semi-detailed explanation:

ls -la /usr/bin/ lists all files in /usr/bin
| pipe (send) the output to the next command
grep -oP matches the search regex per line. o only shows the result not the entire matched line. P tells grep to use perl-regex. I will not go into regex here, read up on it if you want to.
xargs simply put, it gathers the results that are piped to it and send all of them to the end. i.e. to the command following xargs
bash well, it's bash. The c flag tells it to use the string as a command. In this example it loops over the arguments sent from xargs by skipping the first (0th) argument, in this case the loop skips 4.9. The 0th argument is used in the loop to change the link.
ln -s -f The s flag makes a symbolic link, f forces unlinking first if needed.

参考网页,自己去看吧:
https://askubuntu.com/questions/26498/how-to-choose-the-default-gcc-and-g-version
然后,自动执行了下面这些命令:

sudo ln -s -f /usr/bin/g++-11 /usr/bin/g++
sudo ln -s -f /usr/bin/gcc-11 /usr/bin/gcc
sudo ln -s -f /usr/bin/gcc-ar-11 /usr/bin/gcc-ar
sudo ln -s -f /usr/bin/gcc-nm-11 /usr/bin/gcc-nm
sudo ln -s -f /usr/bin/gcc-ranlib-11 /usr/bin/gcc-ranlib
sudo ln -s -f /usr/bin/x86_64-linux-gnu-g++-11 /usr/bin/x86_64-linux-gnu-g++
sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-11 /usr/bin/x86_64-linux-gnu-gcc
sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-ar-11 /usr/bin/x86_64-linux-gnu-gcc-ar
sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-nm-11 /usr/bin/x86_64-linux-gnu-gcc-nm
sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-ranlib-11 /usr/bin/x86_64-linux-gnu-gcc-ranlib

至此,任务完成。

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值