The GNU Compiler Collection (GCC) is a collection of compilers, including C, C++, Objective-C, Fortran, Ada, Go, and D languages. Many open source projects including GNU tools and Linux Kernel are compiled with GCC.
This guide explains how to install the GCC compiler on Ubuntu 18.04. We will show you how to install the stable version and the latest version of GCC.
The same instructions apply to Ubuntu 16.04 and any Ubuntu-based distribution, including Kubuntu, Linux Mint and Elementary OS.
To add new software sources and install software packages on your Ubuntu system, you need to log in to the system as root or another user with sudo privileges.
The default Ubuntu software source contains a software set called build-essential
, which contains the GCC compiler and many other necessary software for compilation.
Perform the following steps to install the GCC compiler on Ubuntu 18.04:
sudo apt update
build-essential
package, enter:sudo apt install build-essential
This command installs some new software, including gcc
, g++
and make
.
You can install the manual to learn how to develop with GNU/Linux:
sudo apt-get install manpages-dev
gcc --version
command, which will print the GCC version information:gcc --version
The default GCC version available on the Ubuntu 18.04 software source is 7.4.0
:
gcc(Ubuntu 7.4.0-1ubuntu1~18.04)7.4.0Copyright(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.
that's it. GCC has been installed on your system, and you can start using it.
Using GCC to compile a basic C or C++ program is very simple. Open your text editor and create the following file:
nano hello.c
# include <stdio.h>
int main(){printf("Hello World!\n");return0;}
Save the file and compile it into an executable file using the following command:
gcc hello.c -o hello
This will create a binary file named hello
in the same directory where you ran the command.
Execute the hello
program:
. /hello
The program should print out:
Hello World!
This chapter provides instructions on how to install and use multiple versions of GCC on Ubuntu 18.04. The new version of the GCC compiler includes support for new languages, better performance, and more new features.
At the time of writing this article, several GCC versions are included in the default Ubuntu software source, ranging from 5.xx
to 8.xx
. The latest GCC version, 9.1.0
is available on Ubuntu ToolChain PPA.
In the following example, we will install the latest three versions of GCC and G++.
First, add the ubuntu-toolchain-r/test
PPA software source to your system:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
To install the GCC and G++ versions you want to install, enter:
sudo apt install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9
The following command will configure alternative and set priority. The default version is the one with the highest priority, in our case it is gcc-9
.
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-990--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-880--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-770--slave /usr/bin/g++ g++/usr/bin/g++-7--slave /usr/bin/gcov gcov /usr/bin/gcov-7
Later, if you want to change your default version, you can use the update-alternatives
command:
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-990 auto mode
1 /usr/bin/gcc-770 manual mode
2 /usr/bin/gcc-880 manual mode
3 /usr/bin/gcc-990 manual mode
Press <enter> to keep the current choice[*], or type selection number:
There should be a list of GCC versions on the system displayed in front of you. Enter the number you want to set as the default version and press the Enter
key.
This command will create a virtual link to the specified version of GCC and G++.
You have successfully installed GCC on your Ubuntu 18.04.
You can browse GCC official documentation page and learn how to use GCC and G++ to compile your C and C++ programs.
Recommended Posts