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:
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:
./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 |
-std=c11 | Selects the C language standard to compile against ( |
-O2 | Turns on a solid level of compiler optimization, typically used for release builds. |
-g | Includes debug symbols so tools like |
-o | Sets the name of the output file produced by the compiler. |
A more complete compile command
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:
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.