The GNU Compiler Collection (GCC) is a compiler software package with a collection of compilers for several languages. It is free and open-source software, which means that everyone has the opportunity to contribute or modify the application according to their own needs. GCC supports various programming languages, including C, C++, Objective-C, Fortran, Java and Ada. It also provides libraries such as libstdc++ for C++ and libgcj for Java.

GCC uses a technology called “Recursive Descent Parsing”, which is very effective at finding errors in the code. GCC also provides a rich set of warnings that can be used to spot possible problems or bugs that may not have been detectable by the compiler itself. GCC performs some optimizations on both the intermediate code and the final machine code, but it does not perform as many optimizations as a commercial compiler would.

The GCC compiler is useful when developing software in various programming languages. GCC helps in having a platform-independent code, which means that the same set of instructions will be translated to the same commands irrespective of what operating system it is being run on. This is achieved by compiling into an intermediate form known as assembly language instead of machine codes specific to a certain architecture or a brand. This assembly code is further converted into machine code by the assembler tool, whose output can be run on any operating system without requiring a recompilation.

This article will show you how to install the GCC compiler on centos 8. It is important for developers who want to write C/C++ programs for Linux operating systems. We will also learn to write a simple program in C/C++ and then compile it using GCC after installing.

Prerequisites

A server running Centos 8. In order to execute the steps in this article, you must have root privileges.

This article assumes that you have a basic understanding of how to use the Linux terminal, and know the basics of compiling programs in C/C++.

Step1. Updating the System

Updating the system is important for the security of our Operating System. If we don’t update it, hackers will find vulnerabilities and exploit them to steal or do damage to our system. Run the following command to update your system.

sudo dnf update -y
sudo dnf clean all

Step 2. Installing GCC Compiler

The CentOS repository comes preinstalled with a package group called “Development tools.” You can also call this tool as GNU Build System.

Development tools are the tools that are required to build applications or libraries for GNU/Linux. The tool provides a great number of libraries and compiler programs for developers. The development tool includes autotools, automake, libtoolize, m4, pkg-config, intltool, GCC, and make.

To check if the development tools are installed on your system, use the following command.

sudo dnf group list

You should get output like this.

Package group list

If the group is not listed, you will need to install it by typing the following command.

sudo dnf group install "Development Tools"

sudo dnf install man-pages

Use the following command if the above command fails.

sudo dnf groupinstall "Development Tools"
sudo dnf install man-pages

Once the compiler and its package are installed, use the following command to show all information about the Development Tools.

sudo dnf group info "Development Tools"

You should get output like this.

Tools in Development Tools group

Now that we have the GCC compiler installed along with the Development Tools group. Let’s verify the GCC version by running the following command.

gcc --version

You should get output like this.

GCC Version

Run the whereis command to find out where the GCC is installed.

whereis gcc

The gcc compiler should be available in the /usr/bin directory by default.

At this point, GCC is installed on your CentOS system.

Step 3. Testing The GCC Compiler

Now that GCC is installed. Let’s test the GCC compiler by creating a simple C++ code, compile it, and execute it using the GCC compiler.

First, create a file called “hello.c” in your home directory with your favorite editor. In this case, the following example uses the nano editor.

cd && sudo nano hello.c

Populate the file with the following content.

#include <stdio.h>

int main() {
  printf("Linuxways, Hello world!\n");
  return 0;
}
  • #include <stdio.h> is a header file required to use printf.
  • int main() is the function where the code resides. In this example, it simply prints “Linuxways, Hello world!” as its output on the screen.
  • { } is where you declare your statements for executing a task or a group of functions. You can also define variables and functions within this block statement.
  • int is a keyword that belongs to the data type int. An integer has no decimal point or fractional component, which can be a positive or negative, whole number or zero.
  • printf( ) function is used to print on the screen.
  • return 0 statement will return an integer value of zero, which is returned back to the main function.

The above program is a simple C++ program to print out “Linuxways, Hello World!” on the Linux terminal. Now that you have created the program, save it by pressing Ctrl+O and press the Enter key for the file name prompt. Then press Ctrl+X to exit the nano editor.

Next, compile the “hello.c” source code with the GCC compiler using the following command.

gcc hello.c -o helloworld
  • The command above takes the source code “hello.c”, and using the GCC compiler, converts the source code into binary executable format or object file and stores it in a location where your system can find it, such as your home directory.
  • -o helloworld is used to give an output file name; you can use any name you like.

When you run the command successfully, you should get a new file called helloworld in your home directory, as shown below.

Compile Hello World C program

We have created and compiled a simple C++ program to print out “Hello World!” using the GCC compiler on your CentOS system. Let’s execute the program using the helloworld executable file which was created above.

./helloworld

If everything goes well, you will see the output “Linuxways, Hello World!” as follows.

Run program

This is a simple example of how to install the GCC compiler on a CentOS 8 Linux system and create a very basic C++ code. With this under your belt, there are many things that you can do with the GCC compiler on CentOS Linux.

Conclusion

In this tutorial, you have learned how to install the GCC compiler on a CentOS 8 Linux system. You have also learned what the GCC is and what are its advantages. Additionally, you have learned how to create a basic program using the GCC compiler on CentOS 8 Linux, which prints the “Hello World!” message. If you have questions or thoughts please share with us.

How to Install GCC Compiler Collection on CentOS 8 and Rocky Linux 8