2024-07-04【Linux】Ubuntu18.04.6升級(jí)gcc/g++11

Ubuntu18.04.6默認(rèn)gcc版本如下:

gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 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++17特性的g++11

安裝

依次輸入以下指令:

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

版本控制

使用指令ls /usr/bin/g++*查看系統(tǒng)的g++數(shù)量及版本

root@ubuntu:~# ls /usr/bin/g++*
/usr/bin/g++  /usr/bin/g++-11  /usr/bin/g++-7
root@ubuntu:~# 

為每個(gè)版本配置替代版本,并將優(yōu)先級(jí)與之關(guān)聯(lián),系統(tǒng)默認(rèn)版本為優(yōu)先級(jí)最高的版本。
最后的數(shù)字為優(yōu)先級(jí),值越大優(yōu)先級(jí)越高

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 90
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 90

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100

現(xiàn)在檢查版本即為優(yōu)先級(jí)最高版本:

root@ubuntu:~# gcc --version
gcc (Ubuntu 11.4.0-2ubuntu1~18.04) 11.4.0
Copyright (C) 2021 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.

切換版本
使用指令sudo update-alternatives --config gcc
出現(xiàn)以下提示

root@ubuntu:~# sudo update-alternatives --config gcc
有 2 個(gè)候選項(xiàng)可用于替換 gcc (提供 /usr/bin/gcc)。

  選擇       路徑           優(yōu)先級(jí)  狀態(tài)
------------------------------------------------------------
* 0            /usr/bin/gcc-11   100       自動(dòng)模式
  1            /usr/bin/gcc-11   100       手動(dòng)模式
  2            /usr/bin/gcc-7    90        手動(dòng)模式

要維持當(dāng)前值[*]請(qǐng)按<回車鍵>,或者鍵入選擇的編號(hào):

輸入對(duì)應(yīng)編號(hào)再按回車即可重新選擇版本

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容