CCompiling with GCC

Compiling with GCC

GCC (the GNU Compiler Collection) is the compiler most C programmers meet first. It's a single command-line tool that quietly runs all four compilation stages — preprocessing, compilation, assembly, and linking — and hands you back a ready-to-run executable.

The basic compile command

Given a source file named file.c, compile it with:

Bash
gcc file.c -o program

-o program tells GCC what to name the output executable. If you omit -o, GCC still compiles successfully but names the result a.out (or a.exe on Windows) — a historical default worth knowing about so it doesn't surprise you. Run the result with:

Bash
./program
# On Windows: program.exe
Hello, World!
Essential flags

Flag

Meaning

-Wall

Enables the main set of useful warnings (unused variables, mismatched types, etc.).

-Wextra

Enables additional warnings not covered by -Wall, catching more subtle mistakes.

-std=c11

Selects the C language standard to compile against (c99, c11, c17, etc.).

-O2

Turns on a solid level of compiler optimization, typically used for release builds.

-g

Includes debug symbols so tools like gdb can map machine instructions back to source lines.

-o

Sets the name of the output file produced by the compiler.

A more complete compile command

Bash
gcc -std=c11 -Wall -Wextra -g file.c -o program

It's common to see this exact combination of flags in real Makefiles and build scripts — it catches mistakes early without changing how the program runs.

Compiling multiple files at once

Real programs are usually split across several .c files. GCC compiles each one into an object file and links them together in a single command:

Bash
gcc -Wall -Wextra main.c helpers.c math_utils.c -o program

GCC figures out which files need recompiling based on what you pass it — for larger projects you'll eventually reach for make or another build system so you don't retype the full file list every time, but for small programs this direct invocation is perfectly fine.

Always enable -Wall -Wextra
C's compiler is, by design, extremely permissive — it will happily compile code that reads uninitialized memory, mismatches pointer types, or ignores a function's return value, and say nothing about it by default. `-Wall -Wextra` turns on the warnings that catch the overwhelming majority of beginner mistakes. Get in the habit of compiling with both flags every single time, and treat warnings as seriously as errors.
Warnings vs errors
A warning does not stop compilation — GCC still produces an executable. That executable may crash, produce wrong output, or exhibit undefined behavior even though the build "succeeded." Read every warning before running the program.