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

Linux cp Command

“cp” is a built-in tool for all the UNIX-based platforms that are used to make copies of the file(s) and directory(s)....
29/12/2020

How To Assign Different Wallpapers For Multi Monitors In Linux

Linux gives immense controls in terms of flexibility of use. Utilizing these controls properly can increase its productivity...
29/12/2020

Hướng dẫn chuyển IPv4 sang IPv6 !

1. Tại sao phải chuyển đổi từ IPv4 sang IPv6? Sự phát triển của công nghệ thông tin và địa chỉ...
27/09/2021
Bài Viết

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

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024