Rocky Linux

How to Install the GCC Compiler on Rocky Linux 9

GNU is the short-term GNU Compiler Collection, with compilers for computing languages such as C, Objective-C, Ada, C++, Go, Fortran, and many more. It is a famous compiler suite widely used in various Linux distros. GNU is developed under the GNU Project, aiming to create an open-source tool to replace traditional compilers.

GCC also supports many runtime libraries, including standard libraries of every programming language, math libraries, etc. That’s why GNU has become a crucial part of the Linux ecosystem, as it provides a versatile compiler suite for various programming languages. So in this guide, you will get to know about the complete method to install and use the GCC compiler in Rocky Linux 9.

How to Install the GCC Compiler on Rocky Linux 9

First, please update the system as per the latest utilities and repositories available for Rocky Linux 9:

sudo dnf update

 

Once you are done, please run the below command to install the GCC in the system:

sudo dnf install gcc -y

 

GCC is the utility in Rocky Linux 9, so the above command will install its current version. Moreover, you can check the installed version of GCC through the following command:

gcc --version

 

In case you want to install GCC-C++, which is its C++ compiler component, please run the following command:

sudo dnf install gcc-c++

 

Now, let’s take a look at the ways to use GCC on Rocky Linux 9 through some examples.

How to Use GCC Compiler [Examples]

Here we will write and compile a C program using the GCC compiler, so please run the below command to open a text editor:

nano num.c

 
After opening the text editor, you can write a program that shows the sum of two numbers:

#include <stdio.h>

int main() {
    int n1, n2, sum;

    printf("Please enter numbers: ");
    scanf("%d %d", &num1, &num2);
    sum = n1 + n2;
    printf(" The sum of given numbers is: %d\n", sum);
    return 0;
}

 

Once you write the program, save and exit the text editor. Now, let’s add the program file to GCC and name it “sum”:

gcc -o sum num.c

 

In the above command, we have used the -o option to show the output filename.

Finally, run the following command to compile the C program right from the terminal:

./sum

 

Similarly, you can compile programs of programming languages, so all you need to do is create a program and add it to GCC.

Wrapping Up

So this was all about the simple way to install and use GCC Compiler on Rocky Linux 9. We have executed a C program to show how to use the GCC Compiler from the terminal. GCC supports various languages, so please follow the above guide to compile those programs with no issues.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.