更新Ubuntu上的GCC

huangapple go评论60阅读模式
英文:

Update GCC on Ubuntu

问题

我正在进行一个需要GCC 10.x或更高版本的项目。

目前我在Ubuntu 20.04.1上安装的是GCC 9.4.0。我尝试更新编译器,但未成功。

有人可以给我升级的建议吗?

我在gcc网站上看到,版本9.4比一些10.x版本更为最新。GCC的结构是怎样的?

此外,我尝试了以下操作:

sudo apt-get install gcc-10.2 g++-10.2

但最终我的GCC版本仍然是9.4。

gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
英文:

I am on a project that needs GCC 10.x or later.

At this time I have GCC 9.4.0 on Ubuntu 20.04.1. I tried to update the compiler, but it does not work.

Can anybody give me an advice for the update?

I read on the gcc website that version 9.4 is more up-to-date than some 10.x versions. How is Gcc structured?

among other things I tried:

sudo apt-get install gcc-10.2 g++-10.2

but after all my gcc version is still 9.4

gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

答案1

得分: 5

这是Linux中的一个常见模式。当同一个程序的多个版本安装时,尽管可执行文件都存在于/usr/bin/目录中,但只有一个版本被视为该程序。例如,如果安装了gcc-9gcc-10,两个可执行文件都存在,分别为/usr/bin/gcc-9/usr/bin/gcc-10,但只有一个版本会被视为gcc。这是通过在/usr/bin/gcc的目录中创建符号链接来实现的。在Ubuntu 20.04中,首选版本是gcc-9,因此将gcc-9创建为gcc的符号链接。

您可以通过运行以下命令来检查这一点。

$ which gcc | xargs file

输出将会是

/usr/bin/gcc: symbolic link to gcc-9

有几种方法可以将gcc-10用作C编译器。

  1. 直接调用gcc-10可执行文件。而不是使用gcc <code.c>,调用gcc-10 <code.c>

  2. 您可以手动创建gcc-10作为首选的gcc符号链接。假设您没有修改系统路径,可以使用以下命令。

# ln -s /usr/bin/gcc-10 /usr/local/bin/gcc

这是因为默认情况下,/usr/local/bin/中的可执行文件优先于/usr/bin/

  1. 如果您使用bash,您可以在您的.bashrc文件中创建一个名为gcc的别名,将其指向gcc-10。添加以下行到您的.bashrc文件中。
alias gcc="gcc-10"

请记住重新启动bash或运行source ~/.bashrc

  1. 使用update-alternatives(感谢@ted-lyngmo指出这一点)。基于Debian的发行版提供了一个单独的程序,可以更轻松地进行符号链接或更多功能的设置。使用man update-alternatives可以了解更多信息。要将gcc-10设置为首选的gcc,使用以下命令。
# update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 60

上述命令表示,/usr/bin/gcc是需要的link,名称是gcc,目标可执行文件是/usr/bin/gcc-10,优先级为60。

这将gcc链接到/etc/alternatives/gcc,它本身是一个指向/usr/bin/gcc-10的符号链接。如果添加了优先级更高的程序到update-alternatives中,/etc/alternatives/gcc将指向较高优先级的程序。

如果没有特殊原因,我建议升级到更新的Ubuntu版本,以便默认的gcc是更新的版本。

我在gcc的官方网站上看到,版本9.4比一些10.x版本更为更新。

新版本的gcc会增加新功能。还会添加对新的C/C++标准的支持。例如,您可以在这里阅读gcc-10的更改内容here。但是,一些程序仍然需要gcc-9,因为只有使用gcc-9才能构建这些程序。因此,GNU会长时间维护gcc-9(以及更旧的版本)。会修复bug并发布更新版本。这可能会在发布了新的gcc版本后发生。因此,gcc-9的某个版本可能比gcc-10的某个版本更新。

英文:

This is a common pattern in linux. When there are multiple versions of the same program installed, though the executables are all present in the /usr/bin/ directory, only one of them is "visible" as that program. For example, if you install gcc-9 and gcc-10, both executables are present as /usr/bin/gcc-9 and /usr/bin/gcc-10 but only one of them is visible as gcc. This happens by symlinking a preferred version to the same directory as /usr/bin/gcc. In ubuntu 20.04, the preferred version is gcc-9 and so, gcc-9 is symlinked as gcc.

You can check this by running the following command.

$ which gcc | xargs file

The output will be

/usr/bin/gcc: symbolic link to gcc-9

There are a few things you can do to use gcc-10 as your c compiler.

  1. Directly call the gcc-10 executable. Instead of using gcc &lt;code.c&gt;, call gcc-10 &lt;code.c&gt;.
  2. You can manually symlink gcc-10 as the preferred gcc. Assuming you did not modify the system paths, the following command can be used.
# ln -s /usr/bin/gcc-10 /usr/local/bin/gcc

This works because, by default, the executables in /usr/local/bin/ take precedence over /usr/bin/.

  1. If you are using bash, you can create an alias for gcc as gcc-10. Add the following line to your .bashrc.
alias gcc=&quot;gcc-10&quot;

Remember to relaunch bash or source ~/.bashrc.

  1. Using update-alternatives (Thanks to @ted-lyngmo for pointing it out). Debian based distributions supply a separate program, that can make symlinking easier / more functional. Read more using man update-alternatives. To use gcc-10 as the preferred gcc, use the following command.
# update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 60

The above command says, /usr/bin/gcc is the link needed and the name is gcc, the target executable is /usr/bin/gcc-10 and it has a priority of 60.

This links gcc to /etc/alternatives/gcc, which itself is a symlink to /usr/bin/gcc-10. If a higher priority program is added to update-alternatives, /etc/alternatives/gcc points to the higher priority program.

If you don't have any specific reason, I would also recommend to upgrade to a newer ubuntu version, so that the default gcc is a newer one.

> I read on the gcc website that version 9.4 is more up-to-date than some 10.x versions.

With newer gcc versions, new features are added. Support for newer c/c++ standards are also added. Eg. You can read the changes for gcc-10 here. But people still need gcc-9 because some programs only build with gcc-9. So, GNU maintains gcc-9 (and much older versions) for a long time. Bugs are fixed, and newer releases are made. This can happen after the release of a newer gcc version. So, it is very much possible that a version of gcc-9 is newer than a version of gcc-10.

huangapple
  • 本文由 发表于 2023年2月8日 18:03:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定