How to install the GCC compiler in Ubuntu 18.04

GNU Compiler Collection (GCC) is a set of C, C ++, Objective-C, Fortran, Ada, Go and the D programming language compiler and libraries. Many open source projects including Linux kernel and GNU tools are compiled with GCC.

The same description applies to any Ubuntu 16.04 and Ubuntu-based distributions, including Kubuntu, Linux Mint and Elementary OS.

Requirement

To be able to add a new repository and install software packages on the Ubuntu system, you must log in as a user with sudo privileges.

GCC installed on Ubuntu

The default Ubuntu repositories contain one yuan package called build-essential, which includes the GCC compiler and many other utilities and libraries required to compile software.

Following steps to install GCC Compiler Ubuntu 18.04:

01, first update the list of packages:

How to install the GCC compiler in Ubuntu 18.04

02, type the following command to install build-essential package:

This command will install a bunch of new packages, including gcc, g ++ and make.

03, to verify that the GCC compiler has been successfully installed, use the Print command gcc --version GCC version:

Ubuntu 18.04 repositories available in the default GCC version 7.4.0:

Export

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.

How to install the GCC compiler in Ubuntu 18.04

that's it. GCC is now installed on your system, you can start using it.

Compile Hello World example

Using the GCC compiler basic C or C ++ program is very simple. Open a text editor and create the following files:

hello.c

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

How to install the GCC compiler in Ubuntu 18.04

Use the following command to save the file and compiled into an executable file:

This creates a binary file called hello in the same directory run the command.

Hello program execution:

The program will show:

Output:

As shown below:

How to install the GCC compiler in Ubuntu 18.04

Install multiple versions of GCC

In this section, we will provide instructions on how to install and use multiple versions of the GCC on Ubuntu 18.04. Newer version of the GCC compiler includes support for new languages, better performance and extended functionality.

As of this writing, the default Ubuntu repository includes several GCC version, from 5.xx to 8.xx latest version of GCC 9.1.0 is available from Ubuntu Toolchain PPA.

In the following example, we will install the latest three versions of GCC and G ++.

First, use the following command / test PPA ubuntu-toolchain-r added to your system:

How to install the GCC compiler in Ubuntu 18.04

How to install the GCC compiler in Ubuntu 18.04

Type the following command to install the required version of GCC and G ++:

How to install the GCC compiler in Ubuntu 18.04

The following command will configure each alternative versions and priority associated with it. The default version is the version with the highest priority is the gcc-9 in our example.

How to install the GCC compiler in Ubuntu 18.04

If you later want to change the default version, use the update-alternatives command:

Output:

There are three candidates GCC may be substituted (provided / usr / bin / gcc).

  Path selection priority state
--------------------------------------------- ---------------
* 0 / usr / bin / GCC-automatic mode. 9 90
  1 / usr / bin / gcc- 7 70 manual mode
  2 / usr / bin / gcc- 8 80 manual mode
  3 / usr / bin / gcc- 9 90 manual mode

To maintain the current value of [*] press <Enter key>, or type selection number:

How to install the GCC compiler in Ubuntu 18.04

You will see a list of all GCC versions already installed on Ubuntu systems. Enter the version number to be used as the default version, and then press Enter.

This command will create a link to a specific version of GCC and G ++ symbolic link.

to sum up

You have successfully installed Ubuntu 18.04 on the GCC. You can now access the GCC official document page to learn how to use the GCC and G ++ to compile C and C ++ programs.

Guess you like

Origin www.linuxidc.com/Linux/2019-06/159059.htm