If you are a newbie in the world of computers and programming languages then it is important to know that your computer cannot “understand” any of the programming languages. The computers interpret only machine languages (ones and zeros). In this situation, a compiler can help you. A computer utilizes compilers for “translating” programming languages into machine language, or we can also say that it converts your source code into an executable file format for your system.

What is g in Ubuntu

The g is a GNU C compiler command utilized to create an executable file through compilation, pre-processing, linking, and assembling source code. There exist many options of the g command that permit us to stop the process at any point along the way.

In the next part of the article, we will explain how to install g in Ubuntu and use it to compile any C source file. So let’s begin!

Note: Login as root or superuser for installing packages and adding repositories to your system.

How to install g in Ubuntu

Now, we will check the method of installing g using the terminal. To do so, open your terminal in Ubuntu by pressing “CTRL ALT T”. Or by searching it manually in the Application’s search bar:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-1.png" data-lazy- height="395" src="data:image/svg xml,” width=”980″>

Update the repositories of your Ubuntu system by using the below-given command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-2.png" data-lazy- height="314" src="data:image/svg xml,” width=”764″>

Now, install g on your Ubuntu by writing out the following command in your terminal:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-3.png" data-lazy- height="484" src="data:image/svg xml,” width=”736″>

Verify the existence of g on your system:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-4.png" data-lazy- height="336" src="data:image/svg xml,” width=”739″>

All done!

How to compile a C script with g

Now, we will create a sample script, and by utilizing g , we will compile it in the terminal. Use nano editor to create and edit the “samplefile.cpp” script:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-5.png" data-lazy- height="286" src="data:image/svg xml,” width=”736″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-6.png" data-lazy- height="485" src="data:image/svg xml,” width=”735″>

Now, add the following lines of code in this “samplefile.cpp” script:

#include

int main()

{


  printf (“This is a test filen);


  return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-7.png" data-lazy- height="481" src="data:image/svg xml,” width=”736″>

Write out the code in the “samplefile.cpp” by pressing “CTRL O”:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-8.png" data-lazy- height="483" src="data:image/svg xml,” width=”740″>

Press “CTRL X” for exiting the nano editor. To run this “samplefile.cpp”, we have to convert “samplefile.cpp” into an executable “samplefile” file. For that, utilize g in this way :

$ g samplefile.cpp -o samplefile

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-9.png" data-lazy- height="304" src="data:image/svg xml,” width=”736″>

Run the executable file “samplefile” in your terminal:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-10.png" data-lazy- height="281" src="data:image/svg xml,” width=”739″>

That’s the method of compiling any C script using g . Now, let’s discuss GCC and how you can use it to compile any C script.

What is GCC in Ubuntu

GCC is an acronym for GNU Compiler Collection. It is a group or collection of libraries and compilers for Fortran, D, C, C , Ada, and Objective-C programming languages. GCC is used to compile many open-source projects, especially the Linux kernel and GNU utilities. It is an important component of the GNU toolchain. It is also considered a standard compiler for most Linux and GNU projects. In 2019, it was declared the most outstanding source project with around 15 million lines of code. GCC is an important tool in the development of free software.

With the help of GCC compilers, when you compile a source code file, the most critical argument to include is the source file’s name. Every other argument is an option, such as linking libraries, debugging, and warnings, etc. GCC commands permit its users to stop the process of compilation at various points. We always recommend the finest option for our readers. Go for GCC installation on your Ubuntu, as it has many libraries and compilers for programming languages, including C .

How to install GCC in Ubuntu

A meta-package named “build-essential” exists in the default repositories of Ubuntu. This package comprises GCC compiler, utilities, and libraries that are needed for compiling any software. If you want to install GCC, write out the below-given command for adding the build-essential package to your system:

$ sudo apt install build-essential

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-11.png" data-lazy- height="287" src="data:image/svg xml,” width=”764″>

Now, verify the existence of the GCC compiler:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-12.png" data-lazy- height="283" src="data:image/svg xml,” width=”765″>

How to compile a C script with GCC

Now, we will compile a “C ” file using the GCC compiler. For that, firstly, we will create a “testfile.cpp” script using the “nano” editor:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-13.png" data-lazy- height="283" src="data:image/svg xml,” width=”736″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-14.png" data-lazy- height="487" src="data:image/svg xml,” width=”737″>

Now, add the following code in your “testfile.cpp” script. When we execute this script, it will print out “This is a test file” on the terminal.

#include

int main()

{


  printf (“This is a test filen);


  return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-15.png" data-lazy- height="481" src="data:image/svg xml,” width=”734″>

Press “CTRL O” to save the “testfile.cpp” script.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-16.png" data-lazy- height="485" src="data:image/svg xml,” width=”739″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-17.png" data-lazy- height="485" src="data:image/svg xml,” width=”738″>

In this step, we will compile the “testfile.cpp” to an executable file “testfile” with the help of GCC:

$ gcc testfile.cpp -o testfile

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-18.png" data-lazy- height="271" src="data:image/svg xml,” width=”739″>

Now, run the executable “testfile” C script:

It will show the following output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/How-to-install-g-on-Ubuntu-19.png" data-lazy- height="315" src="data:image/svg xml,” width=”764″>

Conclusion

Compilers are used for converting source code to an executable file format. Computers and many programming languages utilize these compilers. In Ubuntu, the GCC tool is used; it contains a collection of libraries and compilers for various programming languages, including C, C , Ada. Whereas g is a GNU C and C compiler. We have shown you how to install g and GCC on your Ubuntu system. Moreover, examples are also demonstrated to explain how you can use g and GCC to compile any C source file.

About the author

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

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.