What GCC is
GCC (GNU Compiler Collection) turns source code, mostly C and C++, into programs. It works in four stages:
- Preprocessing. Expands macros and includes header files.
- Compilation. Translates the code into assembly.
- Assembly. Converts the assembly into machine code (object files).
- Linking. Joins the object files and libraries into one program.
Install it
Ubuntu and Debian
sudo apt update

sudo apt install gcc

For C++ add:
sudo apt install g++

Or install the whole set with sudo apt install build-essential.
Fedora and AlmaLinux
sudo dnf update

sudo dnf install gcc

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-alternativesto switch. - You may need to install development libraries (
-devor-develpackages) for the programs you build.
Quick recap
sudo apt install build-essential(Debian/Ubuntu) orsudo dnf install gcc.- Compile with
gcc file.c -o name. - Check with
gcc --version.