在 Ubuntu 16.04 上更新到最新的 gcc 和 g++

在 Ubuntu 16.04 上更新到最新的 gcc 和 g++

我最近尝试降级到 gcc 4.7,因为我正在处理的 cfd 代码无法使用最新的 gcc 版本进行编译。但是在出现许多错误并且代码仍然无法编译之后,我放弃了,并想回到最新的 gcc 和 g++ 编译器,但是似乎我可能搞砸了一些东西。

当我做:

sudo update-alternatives --remove-all gcc 
sudo update-alternatives --remove-all g++
sudo apt-get install gcc-6.2 g++-6.2

甚至 gcc-5.1 g++-5.1 也会出现以下错误

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package gcc-6.2
E: Couldn't find any package by glob 'gcc-6.2'
E: Couldn't find any package by regex 'gcc-6.2'
E: Unable to locate package g++-6.2
E: Couldn't find any package by glob 'g++-6.2'
E: Couldn't find any package by regex 'g++-6.2'

我以前也尝试过这样做

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-6.2 g++-6.2

但似乎不起作用。最后当我这样做

sudo apt-get install gcc

它说

Reading package lists... Done
Building dependency tree       
Reading state information... Done
gcc is already the newest version (4:5.3.1-1ubuntu1).

我知道我仍然需要安装和配置替代方案,但这样做也不起作用,如下所示

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5.3 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5.3 10
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++
sudo update-alternatives --config gcc
sudo update-alternatives --config g++

抱歉,这篇文章太长了,如果您需要更多信息,请告诉我。谢谢!

编辑: 实际上,我通过将上述代码中的 gcc-5.3 更改为 gcc-5 来使其工作,因为这似乎是它在 usr/bin 中的名称。如果有人遇到同样的问题,我会保留它以供将来参考。

答案1

按顺序运行以下命令。我遇到了同样的错误。这对我有用。

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-snapshot
sudo apt-get install gcc-6 g++-6

答案2

必须运行 apt-get update,这样您的计算机才能访问刚刚添加的存储库,从而使其内容可供您使用。如果您不运行它,那么您的计算机就不知道它有可用的存储库信息

相关内容