GNU 编译器集合(GCC)是用于 C、C++、Objective-C、Fortran、Ada、Go 和 D 编程语言的编译器和库的集合。许多开源项目,包括 GNU 工具和 Linux 内核,都是用 GCC 编译的。

本教程介绍了在 Ubuntu 18.04 上安装 GCC 编译器所需的步骤。我们将向您展示如何安装发行版稳定版本和最新版本的 GCC。

相同的说明也适用于 Ubuntu 16.04 和任何基于 Ubuntu 的发行版,包括 Kubuntu、Linux Mint 和 Elementary OS。

先决条件

为了能够在您的 Ubuntu 系统上添加新存储库并安装软件包,您必须以 root 用户或具有 sudo 权限的用户身份登录。

在 Ubuntu 上安装 GCC

默认的 Ubuntu 存储库包含一个名为 build-essential 的元软件包,其中包含 GCC 编译器以及编译软件所需的许多库和其他实用工具。

执行以下步骤以在 Ubuntu 18.04 上安装 GCC 编译器:

  1. 首先更新软件包列表:

    sudo apt update
  2. 通过键入以下命令安装 build-essential 软件包:

    sudo apt install build-essential

    该命令安装了一堆新软件包,包括 gcc、g++ 和 make。

    您可能还想安装有关使用 GNU/Linux 进行开发的手册页:

    sudo apt-get install manpages-dev
  3. 为了验证 GCC 编译器是否成功安装,请使用 gcc --version 命令,该命令会打印 GCC 版本:

    gcc --version

    Ubuntu 18.04 存储库中可用的默认 GCC 版本是 7.4.0:

    gcc (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.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.

就是这样。GCC 现在已安装在您的系统上,您可以开始使用它了。

编译 Hello World 示例

使用 GCC 编译基本的 C 或 C++ 程序非常简单。打开文本编辑器并创建以下文件:

nano hello.c

hello.cc

#include <stdio.h>
int main()
{printf ("Hello World!\n");
  return 0;}

保存文件并使用以下命令将其编译为可执行文件:

gcc hello.c -o hello

这将在您运行命令的同一目录中创建一个名为 hello 的二进制文件。

使用以下命令执行 hello 程序:

./hello

该程序应打印:

Hello World!

安装多个 GCC 版本

本节提供了有关如何在 Ubuntu 18.04 上安装和使用多个 GCC 版本的说明。GCC 编译器的较新版本包括对新语言的支持、更好的性能和扩展功能。

在撰写本文时,默认的 Ubuntu 存储库包含几个 GCC 版本,从 5.x.x 到 8.x.x。最新的 GCC 版本是 9.1.0,可从 Ubuntu Toolchain PPA 获取。

在以下示例中,我们将安装最新的三个版本的 GCC 和 G++。

首先,使用以下命令将 ubuntu-toolchain-r/test PPA 添加到您的系统:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test

通过键入以下命令安装所需的 GCC 和 G++ 版本:

sudo apt install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9

下面的命令将为每个版本配置替代方案并为其关联优先级。默认版本是优先级最高的版本,在我们的情况下是 gcc-9。

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcov gcov /usr/bin/gcov-8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7 --slave /usr/bin/gcov gcov /usr/bin/gcov-7

稍后,如果您想更改默认版本,请使用 update-alternatives 命令:

sudo update-alternatives --config gcc
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-9   90        auto mode
  1            /usr/bin/gcc-7   70        manual mode
  2            /usr/bin/gcc-8   80        manual mode
  3            /usr/bin/gcc-9   90        manual mode

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

您将看到您 Ubuntu 系统上安装的所有 GCC 版本的列表。输入您希望用作默认版本的版本的编号,然后按 Enter。

该命令将创建指向特定版本 GCC 和 G++ 的符号链接。