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

OpenCV Crash Course for Python Developers

Computer Vision and Image Processing can be applied in a lot of areas, and to carry out such tasks a powerful library like...
29/12/2020

Installing OwnCloud on Debian 10

OwnCloud is an open source self-hosted file sharing server. If you want to setup your own file sharing server, then OwnCloud...
29/12/2020

Install Adapta Theme Ubuntu

On Ubuntu, theming is one of the most interesting things you can do to make your system appear COOL. It’s been ages since...
29/12/2020
Bài Viết

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

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

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024