gcc/g++ 10 已安裝,但我無法在 Ubuntu 20.04.2 LTS 上使用它

gcc/g++ 10 已安裝,但我無法在 Ubuntu 20.04.2 LTS 上使用它

我使用了這裡的信息如何在 Ubuntu 18.04 上安裝 g++ 10?在 Ubuntu 20 上安裝 gcc/g++ 10 ,當我嘗試再次執行此操作時:

sudo apt install g++-10

Reading package lists... Done
Building dependency tree       
Reading state information... Done
g++-10 is already the newest version (10.2.0-5ubuntu1~20.04).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

但是當我詢問版本時:

g++ --version

g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.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++-10 --version

g++-10 (Ubuntu 10.2.0-5ubuntu1~20.04) 10.2.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++10 是以 g++-10 的名稱安裝和使用的,但如何使其成為預設的 g++ 編譯器?

答案1

您需要使用update-alternatives來指定預設應使用哪個版本的 g++。就是這樣:

  1. 打開終端機(如果尚未打開)
  2. 配置update-alternatives
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 40
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 60
    sudo update-alternatives --config g++
    

運行最後一個命令後,您將看到一個g++版本選單,您可以選擇預設g++版本。該訊息將如下所示:

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

從選單中鍵入您想要作為預設值的號碼,然後按Enter。如果g++以後需要再次變更預設版本,請再次使用該指令。

筆記:如果您還需要更改 gcc 的預設版本,請遵循相同的流程,替換 g++ gcc不要忘記更改版本號。

希望這可以幫助

相關內容