Install GCC on Ubuntu

29/12/2020
Chưa phân loại
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("%sn", "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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash Split String Examples

We need to split the string data for various purposes in the programming. Many programming languages have a built-in function...
29/12/2020

How to Encrypt/Decrypt files using GPG

Putting strong password on your Windows or Linux lock screen isn’t sufficient now-a-days, because these securities can...
29/12/2020

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 

Thuê mua proxy Viettel là nhu cầu đang được nhiều người dùng đặt ra. Được biết, đây là proxy được...
14/05/2024
Bài Viết

Bài Viết Mới Cập Nhật

Dịch Vụ Xây Dựng Hệ Thống Peering Với Internet Exchange (IXP)
04/04/2025

Dịch Vụ Triển Khai VPN Site-to-Site & Remote Access
04/04/2025

Dịch Vụ Thiết Lập Hệ Thống Tường Lửa (Firewall)
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ảo Hóa & Cloud
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ceph
04/04/2025