While building the Linux kernel, the developers had to build a free and open-source compiler to create the kernel and modules. The GCC compiler was build under the GNU project. In the current version of all Linux distributions, the GCC compiler comes pre-installed inside the operating system. You can use the GCC compiler to compile C, C++, Ada, Go, and a few other object-oriented programming languages. You can compile codes on your terminal shell through the GCC compiler on a Linux system.
GCC Compiler on Linux
The word GCC stands for GNU Compiler Collection. Linux kernel is mostly built on the object-oriented and C programming language. Before installing the GCC compiler on your Linux system, you need to check whether it is already installed on your system or not.
gcc --version
If you get the following message on your shell, you don’t need to install it. If you can’t find the GCC on your machine, you need to install it on your system. This post will see how to install and get started with the GNU Compiler Collection on Linux distributions.
1. Install GCC Compiler on Debian/Ubuntu Linux
On Ubuntu and other Debian distributions of Linux, the GCC compiler comes inside the build-essential packages. The entire package contains GNU C, C++ compiler, and a few more essential library functions and tools. You can run the following aptitude command-line given below to install the GNU Compiler Collection on your Debian Linux distribution. I must note, the following command will require root privileges.
sudo apt install build-essential
2. Install GCC Compiler on Arch Linux
Installing the GCC compiler on an Arch-based Linux system is a straightforward process. You can run the following Pacman command given below on the terminal shell of your Arch Linux system to install the GNU Compiler Collection. Here, I am using the Manjaro KDE Linux to represent the Arch family, and the following command will work on other Arch-based Linux systems as well.
sudo pacman -S gcc
You can also get the GCC compiler on an Arch-based system through the Software Installation and Remove system. You just need to open the software store and search for the GCC compiler. Once you find the package, you are just a few clicks away from removing, installing, or reinstalling it on your system.
3. Install GCC Compiler on Red Hat and Fedora Linux
If you are a Red Hat or a Fedora Linux user, this step is for you. You can install the GCC compiler on your system by running the following command given below. Red Hat Linux users need to run the YUM command with superuser access.
sudo yum groupinstall 'Development Tools'
Fedora Linux users need to run the following DNF command on the terminal shell to get the GNU Compiler Collection on their system.
sudo dnf groupinstall 'Development Tools'
If you face any issues installing the tool, you can run the following command to install the GNU Compiler Collection directly on your Red Hat-based system.
sudo yum install gcc -y
Get Started with the GNU Compiler Collection
Till now, we have how you can install the GNU Compiler Collection on your favorite Linux system. Here, we will see how you can create your first project and run it through the GCC compiler. I’m going to show how you can write a simple code for C programming language to print ‘Hello Ubuntupit.’
First, open your terminal shell, and select a directory to store the code. I’m picking the Desktop directory to save the file. If you don’t choose and directory, it will set the home directory of your Linux filesystem by default. Now, run the following touch command on your terminal shell to create a new file.
$ cd Desktop/ touch hello.c
Once the file is created, open the file using a notepad or script editor. Now copy and paste the following codes given below inside your blank script. Then save and exit the file.
#include <stdio.h> int main() { printf("Hello, UbuntuPIT!"); return 0; }
We can now compile the code through the GCC compiler on our Linux system. Run the following command on your terminal shell to compile the code. You can give a new name to your code while compiling. Here, I’m naming the compiled file as Ubuntupit. We will later use the name to run the code. We can run the C programming code on our Linux terminal through the dot slash (./) command on the terminal shell.
Run the following command on your terminal shell to execute the compiled file through the GCC compiler on your Linux system. Here the -o
flag is used to indicate the output file.
$ cd Desktop/ gcc hello.c -o UbuntuPIT ./UbuntuPIT
You can see that the code has successfully compiled and run through the GCC compiler on a Linux system.
Remove GCC Compiler from Linux
You might need to remove the GNU Compiler Collection from your Linux system if you find any broken package or repository issues. Here is the process how you can remove it from your Linux machine. Run the appropriate command from the following command-lines to remove the GCC compiler on your Linux system.
You can remove GNU Compiler Collection from Debian/Ubuntu Linux by running the following command.
sudo apt-get install --skip-broken gcc
Run the following command to remove GNU Compiler Collection from Red Hat and Fedora Linux.
sudo yum remove --skip-broken gcc
Arch Linux users need to run the following Pacman command to remove the broken GNU Compiler Collection package.
sudo pacman -R gcc
To remove the GNU Compiler Collection from Arch Linux, you can also use the default software center method that I have described previously.
Final Words
Using GCC compiler is the most effortless method to build and run the C program on any Linux system. If you are a newbie on programming or love the free and open-source software, you will enjoy using the GNU Compiler Collection. In the entire post, I have described the method of installing the GCC compiler on Linux distributions and how you can get started with it.
Please share it with your friends and the Linux community if you find this post useful and informative. You can write to us with your opinions regarding this post in the comment section.