The GNU Compiler Collection, commonly known as (GCC), is a software package with compilers and development tools for several programming languages, including C, C++, Fortran, Go, Objective-C++, Objective-C, among others. It is available for various platforms, including Windows, Linux, and several BSD-based operating systems.
Why is GCC Popular in Linux Systems?
Though we will be putting a lot more focus on Rocky Linux in this post, if you have used other Linux distributions, you must have come across GCC. As of writing this post (2021), the GNU Compiler Collection is the most used compiler in the open-source world. It is the default compile for the Linux kernel source code and delivers stable performance for the specified hardware. It is also used to build and compile most system libraries and applications for Linux systems.
Additionally, developers utilize the Linux kernel to build embedded systems that highly rely on GCC to compile and develop apps/libraries for the ever-growing field of IoT (Internet of Things).
In this post, we will give you a step-by-step guide on installing GCC on Rocky Linux. We will also write a small C/C++ program and compile it with GCC. To finish the party, we will give you an in-depth understanding of how GCC carries out compilation. Let's dive in.
Requirements
- A PC running Rocky Linux server or the GUI Desktop version
- An active internet connection
- You will require root privileges to execute some of the steps.
This post assumes you have a good understanding of the Linux terminal. If you are just getting started with Linux systems, please look at our article - 100+ Linux commands cheat sheet & examples, which gives you a list of Linux commands with samples and the man page link to provide you with an overview of Linux day-to-day usage.
Step 1: Update the System
As a rule of thumb with Linux systems, you need to update your system regularly by running the update command on your Terminal. That ensures having the latest system packages patched any vulnerabilities or performance issues reported by the community. To update Rocky Linux, run the commands below on the Terminal.
sudo dnf update
sudo dnf clean all
Step 2: Install GNU Compiler Collection (GCC) on Rocky Linux
Rocky Linux and many other RedHat-based Linux distributions come with a package called "Development tools" used for building and compiling software/ libraries on your GNU/Linux system. This package contains GCC, g++, make, libtool, rpmbuild, Autoconf, etc. You can check if the "development tools" package is available for your system by running the command below.
sudo dnf group list
You will see an output of all packages available for install on your system. To install the "Deevelopment tools," run the command below on your Terminal.
sudo dnf update
sudo dnf groupinstall "Development Tools"
If that command doesn't work for you, run the command below.
sudo dnf group install "Development Tools"
After successfully installing "Development tools," run the command below to view the goodies that come with this package.
dnf groupinfo "Development Tools"
The image above shows that the GCC compiler was successfully installed along with the "Development tools" package.
Step 3: Check GCC Version and Installation Directory
Now that GCC is installed successfully, you need to verify the version to ensure it is compatible with the system or library you wish to install. Execute the command below:
gcc --version
To check where GCCis installed on your system, run the command below:
whereis gcc
Step 4: Test Your GCC Compiler
To ensure our GCC compiler is running well on our system, we will write a simple C program called.'addNum.c
' that adds two numbers entered by the user. See the code below:
#include <stdio.h> int main() { int a; int b; int sum; printf ("Enter the first number: "); scanf ("%d", &a); printf ("Enter the second number: "); scanf ("%d", &b); sum = a + b; printf ("%d", sum); return 0; }
To run the program, we will use the syntax below:
gcc <program-name>
e.g
gcc addNum.c
Now, GCC will create a file called a.out
which is a low-level code that your computer can understand. When you run the ls
command, you should see this file, as shown in the image below.
Run the a.out
file using the command below:
./a.out
From the image above, you can see the program prompted us to enter the first and second numbers before adding them together and printing the answer on our Terminal. That's quite interesting. However, let even explore GCC more and understand how it carries out compilation.
Understanding How GCC Carries Out Compilation
In Step 4 above, we wrote a simple program that we compiled with GCC to get a a.out
machine-readable file. That looked easy and fast, but under the hood, several processes took place. We won't dive into the complex part, but we will look at the compilation process from a beginner's perspective. Let's dive in.
- Pre-processing: GCC first takes the header files and includes them in your source code. It then expands the inline of your macros before stripping all the comments in your code.
- Compilation: This is the most complex part. GCC first does a semantic analysis of your code, checks it for errors, removes dead code, removes unreachable code, and if you specify a type of optimization, it goes ahead and runs those passes.
- Assembly: AT this point, your code is now in Assembly. Here it is converted into an actual object file.
- Linking: The linker combines the object file with any other libraries present, including the C standard library. From this point, you now have an executable file (a.out) that you can run on your system.
Conclusion
This post has given you a step-by-step guide on installing GCC on Rocky Linux. We also went further and wrote a C program to add two numbers, and further looked at how GCC carries out compilation. Do you have any questions or suggestions? Please feel free to hit the comments section below.