Ubuntu

Install GCC on Ubuntu

The full form of GCC is GNU Compiler Collection. It is an open source toolset for compiling source codes of C, C++, Objective-C, Fortran, Ada, Go and D programming languages.

In this article, I am going to show you how to install GCC on Ubuntu and compile C and C++ programs. So, let’s get started.

Installing GCC:

GCC and all the required build tools can be installed very easily on Ubuntu as all the required packages are available in the official package repository of Ubuntu. Ubuntu also provides the build-essential meta package that installs all the required packages all at once. So, you can easily GCC on Ubuntu using the APT package manager.

First, update the APT package repository cache with the following command:

$ sudo apt update

The APT package repository cache should be updated.

Now, install the build-essential package with the following command:

$ sudo apt install build-essential

Now, press y and then press <Enter> to confirm the installation.

It will take a while for APT to download and install all the required packages from the official Ubuntu package repository.

At this point, GCC and all the required build tools should be installed.

In the next sections of this article, I am going to show you how to compile a simple C and C++ program with GCC.

Compiling C Programs with GCC:

In this section, I will write a simple C program, show you how to compile the C program with GCC and run the compiled program.

I have written a simple C source file and saved it as hello.c in the ~/Projects directory. The contents of the hello.c file is as follows:

#include <stdio.h>
 
int main(void) {
printf("%s\n", "C -> Welcome to LinuxHint!");
 
return 0;
}

This program will print “C -> Welcome to LinuxHint!” on the terminal. Very simple.

Before you compile the C source file, navigate to your project directory (~/Projects in my case) as follows:

$ cd ~/Projects

Now, to compile the hello.c C source file, run the following command:

$ gcc hello.c  -o hello

NOTE: Here, hello.c is the C source file. The -o option is used to define the path and filename of the compiled output binary file. -o hello is used to tell GCC that the compiled output file should be hello and the path where the file will be saved is the current working directory.

Once you compile the hello.c source file, a new file hello will be generated as you can see in the screenshot below. This is the compiled binary file.

Now, run the hello binary file as follows:

$ ./hello

As you can see, the correct output is displayed on the terminal. So, we’ve successfully compiled and ran a C program using GCC.

Compiling C++ Programs with GCC:

In this section, I will write a simple C++ program, show you how to compile the C++ program with GCC and run the compiled program.

I have written a simple C++ source file and saved it as helloworld.cpp in the ~/Projects directory. The contents of the helloworld.cpp file is as follows:

#include <iostream>

 

using namespace std;

 

int main(void) {

cout << "C++ -> Welcome to LinuxHint!" << endl;

 

return 0;

}

This program will print “C++ -> Welcome to LinuxHint!” on the terminal. Very simple as in the last example.

Before you compile the C++ source file, navigate to your project directory (~/Projects in my case) as follows:

$ cd ~/Projects

Now, to compile the helloworld.cpp C++ source file, run the following command:

$ g++  helloworld.cpp -o helloWorld

NOTE: Here, helloworld.cpp is the C++ source file. The -o option is used to define the path and filename of the compiled output binary file. -o helloWorld is used to tell GCC that the compiled output file should be helloWorld and the path where the file will be saved is the current working directory.

Once you compile the helloworld.cpp C++ source file, a new file helloWorld will be generated as you can see in the screenshot below. This is the compiled binary file.

Now, run the helloWorld binary file as follows:

$ ./helloWorld

As you can see, the correct output is displayed on the terminal. So, we’ve successfully compiled and ran a C++ program using GCC.

So, that’s how you install GCC on Ubuntu and compile C and C++ programs with it. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.