Before we begin talking about how to install GCC Compiler on Ubuntu 20.04. Let’s briefly understand – What is a GCC Compiler?
The GNU Compiler Collection is mainly a collection of compilers as well as of libraries for C, C++, and Objective-C. There are many open-source projects, like GNU tools and the Linux kernel, that are compiled with GCC.
In this tutorial, you will install the GCC Compiler on Ubuntu 20.04.
The build-essential meta-package is a package available on Ubuntu that includes five separate packages essential during the software compilation process. It also contains the GCC compiler, which is required to build and run C programs. The five packages included in the build-essential package are as follows:
1) You must log in as root or user with the sudo privileges.
1) Here, the default Ubuntu repositories have a meta-package as build-essential . It contains the GCC compiler and many libraries and utilities necessary for compiling the software. Perform the below steps to install GCC Compiler on Ubuntu 20.04.
2) Firstly, start by updating the packages list:
3) After that, install build-essential package by typing:
sudo apt install build-essential
The command will install a bunch of new packages. It will also include gcc , g++ and make . You may also need to install the manual pages. It is about using GNU/Linux for the development:
sudo apt-get install manpages-dev
4) To validate if the installation of the GCC compiler is successful, you will use gcc —version command. It will print the GCC version:
5) 9.3.0 is a default version of GCC available in Ubuntu 20.04 repositories.
Output gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0 Copyright (C) 2019 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 compiler installation on your system is now successful. You can now start using it.
1) To Compile a basic C or C++ program, you will do it using GCC. Open your text editor and create the below file:
2) Proceed to save the file. Then, compile it into an executable using the below command:
3) It will create a binary file with a name hello in the same directory. It is where you run the command. Now, execute the hello program, by:
1) Here, you will now install and use multiple versions of GCC on Ubuntu 20.04. The latest versions of the GCC compiler have new functions and optimization improvements.
Further, the default Ubuntu repositories have several GCC versions. It is from 7.x.x to 10.x.x . Now, you will install the latest three versions of GCC and G++.
2) Therefore, first, add ubuntu-toolchain-r/test PPA to your system using the following command:
sudo apt install software-properties-common sudo add-apt-repository ppa:ubuntu-toolchain-r/test
3) Next, install the necessary GCC and G++ versions by:
sudo apt install gcc-8 g++-8 gcc-9 g++-9 gcc-10 g++-10
4) Then, the command below will configure an alternative for each version and associate a priority with it. The default version is the one with the highest priority. So, here is the gcc-9 :
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10 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
5) Later, if you will need to change the default version, you can do it using the update-alternatives command:
sudo update-alternatives --config gcc
Output There are 3 choices for the alternative gcc (providing /usr/bin/gcc). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/gcc-10 100 auto mode 1 /usr/bin/gcc-10 100 manual mode 2 /usr/bin/gcc-8 80 manual mode 3 /usr/bin/gcc-9 90 manual mode Press to keep the current choice[*], or type selection number:
6) You will see a list of all installed GCC compiler versions on your Ubuntu system. Then, enter the number of versions you want to use as a default. Proceed to press Enter . The command will create symbolic links which will be to the specific versions of GCC and G++.
You need to use the which command to locate c compiler binary called gcc. Usually, it is installed in /usr/bin directory.
To find the location of the Linux C Compiler, launch a terminal and then type the following commands:
which g++
which cc
which gcc
You can use the following command to verify if GCC is installed on your PC:
If GCC is installed on your computer, a variety of data should be shown, beginning with the phrase Using built-in specs which indicates that GCC is installed on your PC.
We hope this detailed tutorial helped you to install the GCC Compiler on Ubuntu 20.04.
If you have any queries or doubts, please leave them in the comment below. We’ll be happy to address them.
Adblock