Installing GCC and C/C++ Build Tools on CentOS 8

29/12/2020
Chưa phân loại
In this article, I am going to show you how to install GCC and all the required C/C++ build tools on CentOS 8 for developing C/C++ programs. So, let’s get started.

Installing GCC and C/C++ Build Tools:

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

$ sudo yum makecache

The YUM package repository cache should be updated.

On CentOS 8, all the C/C++ development tools can be installed very easily by installing the Development Tools group.

$ sudo yum grouplist

To install the Development Tools group of packages, run the following command:

$ sudo yum groupinstall "Development Tools"

To confirm the installation, press Y and then press <Enter>.

YUM package manager should download all the packages from the internet and install them on your CentOS 8 machine.

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

To confirm whether GCC is working correctly, run the following command:

$ gcc –version

As you can see, GCC is working correctly.

Now, to check whether G++ is working correctly, run the following command:

$ g++ –version

As you can see, G++ is working correctly.

To check whether make tool is working correctly, run the following command:

$ make –version

As you can see, make is working correctly.

Writing Your First C and C++ Program:

In this section, I am going to show you how to write your first C and C++ program, compile them using GCC and run them. So, let’s continue,

NOTE: A C program source file must end with the extension .c and C++ program source file must end with the extension .cpp. You must always remember that.

First, create a C program source file hello.c and type in the following lines of codes.

#include <stdio.h>
#include <stdlib.h>
 
int main(void) {
  printf("Hello world from LinuxHint!n");
 
  return EXIT_SUCCESS;
}

The final source code file should look like this.

Once you’ve written your C program, navigate to the directory (in my case ~/codes directory) where you saved the hello.c C source file as follows:

$ cd ~/codes

As you can see, the hello.c C source file is in this directory.

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

$ gcc hello.c

If you don’t specify a name for the compiled binary/executable file, a.out will be the default name for the compile binary/executable file.

If you want to give your compiled binary/executable file a name i.e. hello, compile the C source file hello.c with the following command:

$ gcc -o hello hello.c

NOTE: Here, -o option defines the output file or compiled binary/executable file name.

Once the C source file hello.c is compiled, a new compiled binary/executable file hello should be generated as you can see in the screenshot below.

$ ls -lh

Now, run the compiled binary/executable file hello as follows:

$ ./hello

As you can see, the desired output is printed on the screen.

Now, create a new C++ source file hello.cpp and type in the following lines of codes.

#include <iostream>
 
using namespace std;
 
int main(void) {
  cout << "C++: Hello world from LinuxHint!" << endl;
 
  return EXIT_SUCCESS;
}

The final source code file should look like this.

As you can see, the hello.cpp C++ source file is in the ~/codes directory.

$ ls -lh

Now, compile the C++ source file hello.cpp and give the compiled binary/executable file a name hello-cpp with the following command:

$ g++ -o hello-cpp hello.cpp

Once the C++ source file hello.cpp is compiled, a new compiled binary/executable file hello-cpp should be created as you can see in the screenshot below.

Now, run the hello-cpp compiled binary/executable file as follows:

$ ./hello-cpp

As you can see, the desired output is printed on the screen.

So, that’s how you install GCC and C/C++ build tools on CentOS 8 and write your first C/C++ programs. 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

SQLite Tutorial

Ever since man started creating systems, there have always been databases corresponding to them. Fast forwarding to our...
28/12/2020

How to do a Port Scan in Linux

Port scanning is a process to check open ports of a PC or a Server. Port scanners are often used by gamers and hackers...
29/12/2020

[CVE-2019-12949] Khai thác lỗ hổng XSS trong pfSense 2.4.4

Mô tả Trong phiên bản pfSense 2.4.4 kẻ tấn công có thể thực hiện bất kì command nào với quyền...
30/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