While working as an application developer, it is common practice that some projects require different compiler versions for handling source code. With the rapid increase of software technology, you often find yourself where you need to use a specific compiler for a project. Today, different compiler versions are using for compiling the C programs. Here, we will talk about the G GNU compiler; a Linux system compiler specifically used to compile C programs. The file extensions of these programs are .c and .cpp that are compiled using the G compiler.

This article aims to provide a detailed guide on installing and using the G compiler on the Ubuntu 20.04 LTS system. You can achieve this goal by installing some development tools named build-essential packages on your system.

Installation of g on Ubuntu 20.04 system

Perform the below-mentioned steps to install g on the Ubuntu system:

Step 1: Install build-Essentials

The Ubuntu official default repository contains a number of development tools or packages. These tools you can install directly by installing a Ubuntu meta-package named “build-essential” on your system that includes the important development libraries, GNU debugger, and compiler collections required for compiling C/C applications. Therefore, it is required to install build-essentials on your system before installing any compiler. Type the following terminal command to install development tools:

$ sudo apt update

$ sudo apt install build-essential

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image7-13.png" data-lazy- height="760" src="data:image/svg xml,” width=”1106″>

The above meta-package installs the number of packages including g automatically, gcc, and make.

Step 2: Check g compiler version

Once all development packages are installed, type the following command to check the installed version of the g compiler:

The latest installed g version displays on the terminal as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image9-10.png" data-lazy- height="195" src="data:image/svg xml,” width=”1086″>

Install multiple g compiler versions on Ubuntu 20.04

First, install build-essentials development tools and then use the following command to install multiple g compilers:

$ sudo apt install build-essential

$ sudo apt -y install g –7 g –8 g –9 g –10

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image8-13.png" data-lazy- height="654" src="data:image/svg xml,” width=”1104″>

The above command will complete in a few minutes after installing all g compiler versions on your Ubuntu system.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image11-8.png" data-lazy- height="655" src="data:image/svg xml,” width=”1102″>

Create a list of g alternatives using update-alternatives tools

The update-alternative tool is used to create the list of g multiple compiler alternatives. Using the below-mentioned commands, user can create the multiple g alternatives list on Ubuntu system:

$ sudo update-alternatives –install /usr/bin/g g /usr/bin/g –7 7

$ sudo update-alternatives –install /usr/bin/g g /usr/bin/g –8 8

$ sudo update-alternatives –install /usr/bin/g g /usr/bin/g –9 9

$ sudo update-alternatives –install /usr/bin/g g /usr/bin/g –10 10

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image10-9.png" data-lazy- height="439" src="data:image/svg xml,” width=”1104″>

Now, display the list of g alternatives on your Ubuntu system by using the following update-alternatives command:

$ sudo update-alternatives –config g

Check the available g version in the list that would display on the terminal as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image2-28.png" data-lazy- height="325" src="data:image/svg xml,” width=”1104″>

Choose the g compiler version that you want to use as the default compiler for software development. Press ‘enter’ to keep the current choice set as default or select the number from the g alternatives list.

For example, to use the g -9 version set as default, enter the number ‘4’. Hence, this version will be set as default. Similarly, if you want to switch to another g version then, repeat the configuration process and select the desired g version from the alternatives.

Installing g and gcc compiler in a single command

Most Linux users want to install multiple gcc and g compiler alternatives on the same system. In this case, you can also install gcc and g compilers using just a single command on your Ubuntu system.

$ sudo apt -y install gcc-7 g –7 gcc-8 g –8 gcc-9 g –9

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image1-28.png" data-lazy- height="702" src="data:image/svg xml,” width=”1100″>

Follow the same procedure for configuring gcc compiler alternatives. Use the update-alternatives command to create the list of gcc compilers.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image4-24.png" data-lazy- height="325" src="data:image/svg xml,” width=”1105″>


After that, select the default gcc version or alternative using the ‘config gcc’ command, which is also mentioned above.

$ sudo update-alternatives –config gcc

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image3-27.png" data-lazy- height="296" src="data:image/svg xml,” width=”1102″>

How to use of g on Ubuntu 20.04?

Compiling a C program using the g is quite easy and simple. We will create and execute the source code of the c program.

For example, to display a message ‘Hello, Welcome to the LinuxHint tutorials!’ create the following source code in a text file:

#include

using namespace std;

int main()

{

    cout << “Hello, Welcome to the LinuxHint Tutorials!n;

    return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image6-16.png" data-lazy- height="375" src="data:image/svg xml,” width=”1087″>

Now, save the above source code and rename the file with ‘welcome.cc’. You can assign any name to this file with the ‘.cc’ extension.

Compile the above source code using the g compiler by running the following command:

$ g -o welcome welcome.cc

The above command creates a binary file named ‘welcome’ in the current directory where you run the command. Execute the ‘welcome’ executable program by using the following command:

The following message should display on the terminal after running the above command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image5-20.png" data-lazy- height="111" src="data:image/svg xml,” width=”1103″>

Conclusion

We learned about the installation of g on the Ubuntu 20.04 system in this article. We demonstrated how to install different g compiler versions at the same time on a Ubuntu system. Moreover, we have also explained how to compile a program using the g compiler. I hope all the above information about g is enough for your understanding. Visit GNU C compilers to get more details about Gcc or C compilers. Send us your feedback about this tutorial through comments.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/49ac9cf0c583f1d7e06205a87f549d76?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Samreena Aslam

Samreena Aslam holds a master’s degree in Software Engineering. Currently, she’s working as a Freelancer & Technical writer. She’s a Linux enthusiast and has written various articles on Computer programming, different Linux flavors including Ubuntu, Debian, CentOS, and Mint.