Site Tools


wiki:gcc-basics

Table of Contents

GCC Basics

GCC is the GNU Compiler Collection—the default C, C++, and Fortran compiler on most Linux systems. Install it via your package manager along with build tools.

sudo apt install build-essential              # Debian/Ubuntu
sudo dnf groupinstall "Development Tools"     # Fedora/RHEL

build-essential includes gcc, g++, make, and C library headers. Check the installation:

gcc --version
gcc -dumpmachine          # target architecture

Compilation pipeline: GCC runs four stages internally but hides them by default:

gcc -E file.c -o file.i     # preprocess (expand macros, includes)
gcc -S file.c -o file.s     # compile to assembly
gcc -c file.c -o file.o     # assemble to object code
gcc file.o -o program       # link object files and libraries

Or combine them into one command: gcc -o program file.c. GCC automatically runs all stages and produces the executable.

Multi-file projects:

gcc -c a.c -o a.o           # compile each source separately
gcc -c b.c -o b.o
gcc a.o b.o -o program      # link objects together

This is the basis for makefiles—compile only changed files, relink to save time.

Common flags:

gcc -o output input.c       # output filename
gcc -g input.c              # include debug symbols
gcc -O2 input.c             # optimization level 2
gcc -Wall input.c           # enable common warnings
gcc -I/path/to/headers input.c   # add include path
gcc -L/path/to/libs -lmylib input.c  # link against library

-o names the output; without it, the default is a.out. -g includes debugging information (needed for GDB). -O sets optimization level. -I adds header search paths; -L and -l link against libraries.

Variables used by make:

CFLAGS=-O3 -Wall -g        # compiler flags
LDFLAGS=-L/path/to/libs    # linker flags
gcc $CFLAGS -o app app.c
gcc -c $CFLAGS app.c
gcc app.o $LDFLAGS -o app

These can be set on the command line or in a Makefile, making build configuration centralized and reproducible.

wiki/gcc-basics.md · Last modified: by 127.0.0.1