Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Install the GCC Compiler on Linux

Install GCC and G++ on Ubuntu, Debian, Fedora, AlmaLinux, Arch and CentOS, and compile your first program.

VPS and Cloud Servers2 min read4 steps5 screenshots

What GCC is

GCC (GNU Compiler Collection) turns source code, mostly C and C++, into programs. It works in four stages:

  1. Preprocessing. Expands macros and includes header files.
  2. Compilation. Translates the code into assembly.
  3. Assembly. Converts the assembly into machine code (object files).
  4. Linking. Joins the object files and libraries into one program.

Install it

Ubuntu and Debian

sudo apt update
Updating the package list
sudo apt install gcc
Installing gcc

For C++ add:

sudo apt install g++
Installing g++

Or install the whole set with sudo apt install build-essential.

Fedora and AlmaLinux

sudo dnf update
Updating with dnf
sudo dnf install gcc
Installing gcc with dnf

For C++: sudo dnf install gcc-c++.

Arch Linux

sudo pacman -Syu
sudo pacman -S gcc

CentOS and RHEL

sudo yum install epel-release
sudo yum update
sudo yum install gcc

Check the install

gcc --version

Use GCC

The basic form:

gcc [options] files

Compile one file

gcc hello.c -o hello
./hello

-o sets the name of the program.

Compile several files

gcc main.c utils.c -o myprogram

Link a library. -lm links the math library:

gcc myprogram.c -o myprogram -lm

Debug with GDB. Add -g:

gcc -g myprogram.c -o myprogram
gdb myprogram

Optimize

gcc -O2 myprogram.c -o myprogram

Show all warnings

gcc -Wall -Wextra myprogram.c -o myprogram

A first program

Create hello.c:

#include <stdio.h>

int main(void) {
    printf("Hello, world!\n");
    return 0;
}

Notes

  • Several GCC versions can live side by side. Use update-alternatives to switch.
  • You may need to install development libraries (-dev or -devel packages) for the programs you build.

Quick recap

  • sudo apt install build-essential (Debian/Ubuntu) or sudo dnf install gcc.
  • Compile with gcc file.c -o name.
  • Check with gcc --version.