How to Install GCC Compiler on Linux

GCC (GNU Compiler Collection) is a widely used compiler system that supports various programming languages, including C, C++, Objective-C, Fortran, Ada, and Go. It is a fundamental tool for Linux developers allowing them to compile and link their source code into executable programs.

In this post, we will cover the process steps involved in installing GCC on different Linux distributions.

Understanding GCC

Before we start to install GCC Linux process let’s briefly understand what is GCC compiler. It’s a suite of compilers, not a single compiler. Each language has its own compiler within the GCC suite. When you compile your code using GCC it goes through several stages:

  1. Preprocessing: The preprocessor expands macros, includes header files, and handles conditional compilation directives.
  2. Compilation: The compiler translates the preprocessed code into assembly language.
  3. Assembly: The assembler converts the assembly code into machine code.
  4. Linking: The linker combines the object files like machine code with libraries to create the final executable.

Installing GCC on Different Distributions

The exact steps for GCC compiler installation may vary slightly depending on the Linux distribution you are using. Here are the instructions for some common distributions:

Ubuntu or Debian

To update the package list on Ubuntu or Debian system use the following command:

sudo apt update
apt update

Then install the GCC with the help of the following command:

sudo apt install gcc
install gcc

To install GCC for a specific language for example C++ use the following:

sudo apt install g++
install g++

Fedora or Alma Linux

To update the package list on the Fedora system use the following command:

sudo dnf update
update command

Then install the GCC with the help of the following command:

sudo dnf install gcc
dnf install gcc

Arch Linux

To update the package list on Ubuntu or Debian system use the following command:

sudo pacman -Syu

Then install the GCC with the help of the following command:

sudo pacman -S gcc

CentOS or RHEL

First, install or enable the EPEL repository if not already enabled:

sudo yum install epel-release

To update the package list on CentOS or RHEL system use the following command:

sudo yum update

Then install the GCC with the help of the following command:

sudo yum install gcc

If you are using a less common distribution refer to its package manager for specific GCC compiler Linux download. The general approach is usually similar involving updating package lists and installing the GCC package.

Verifying GCC Installation

To verify that GCC is installed correctly you can check its version:

gcc --version

This will output the installed GCC version.

Using GCC on Linux

Once you have successfully installed GCC on your Linux system you can start using it to compile your C/C++ programs. Here’s a basic overview of the GCC command and some common usage scenarios:

Basic Command

The general syntax for using GCC is:

gcc [options] files
  • options: These are optional flags that control the compilation process.
  • files: This specifies the input files like source code files you want to compile.

Compile Single C File

To compile a single C file named hello.c into an executable named hello you would use:

gcc hello.c -o hello

The -o option specifies the output filename.

Compile Multiple C File

To compile multiple C files for example main.c and utils.c into a single executable:

gcc main.c utils.c -o myprogram

Linking with Libraries

If your program uses external libraries you need to link them during compilation. For example, to link with the math library:

gcc myprogram.c -o myprogram -lm

The -lm flag links with the math library.

Debugging with GDB

To compile your program with debugging symbols for use with GDB:

gcc -g myprogram.c -o myprogram

Then, you can use GDB to debug the executable:

gdb myprogram

Optimizing Code

To enable optimizations for example for performance:

gcc -O2 myprogram.c -o myprogram

The -O2 flag enables level 2 optimizations.

Important Notes

Following are the important notes while installing the GCC Compiler on a Linux system:

  • Some distributions allow you to install multiple GCC versions. This can be useful for compatibility reasons or when working on projects that require specific GCC features.
  • GCC supports various compiler flags that can be used to customize the compilation process. For example, you can use flags to enable optimizations disable warnings, or specify the target architecture.
  • GCC often comes with a debugger (like GDB) that can be used to step through your code, inspect variables, and identify errors.
  • In addition to the compiler, you might need to install libraries that your programs depend on. For example, if your program uses the math.h header file you will need to ensure that the math library is installed.

Conclusion

GCC is a powerful and versatile compiler suite that plays an important role in the development of C, C++, and other programming languages on Linux systems. By following the steps outlined in this guide you should be able to successfully install and use GCC to compile and build your projects.

Install GCC Compiler on Linux is a simple process depending on different Linux enviourment. For a more robust and flexible environment consider Ultahost’s cheap VDS hosting which provides dedicated resources and optimal performance ideal for managing advanced process tasks.

FAQ

What is GCC?
Why do I need GCC on Linux?
How do I check if GCC is already installed on my Linux system?
How can I install GCC on Ubuntu?
Is GCC pre-installed on all Linux distributions?
Can I install GCC on Linux without admin rights?
How do I update GCC to the latest version?

Related Post

Exploring Grep Command in Linux

When you are a Linux user, few tools commands are as po...

How to Install Deb Files / Packages on Ubuntu

The .deb packages are the standard format for installin...

How to Install Objective-C on Linux

Objective-C is a powerful programming language often as...

How to Install Visual Studio Code on Linux

Visual Studio Code (VS Code) is a widely used code edit...

How to Use the ulimit Linux Command

ulimit stands for "user limits" and is used to set or d...

Guide to Installing Commands on CentOS

When a Windows user switches to Linux the first thing i...

Leave a Comment