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

the*_*guy 14 apt 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
Run Code Online (Sandbox Code Playgroud)

甚至 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'
Run Code Online (Sandbox Code Playgroud)

我以前也试过这样做

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-6.2 g++-6.2
Run Code Online (Sandbox Code Playgroud)

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

sudo apt-get install gcc
Run Code Online (Sandbox Code Playgroud)

它说

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

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

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++
Run Code Online (Sandbox Code Playgroud)

对不起,这结束了很长时间,如果您需要更多信息,请告诉我。谢谢!

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

小智 7

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

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
Run Code Online (Sandbox Code Playgroud)