Loading...

C essentialsGetting Started

How compilation works: preprocess, compile, link

8 min lesson

Next
  • Compilation is the process of turning human-readable C source code into an executable file the computer can run.
  • It happens in four stages: preprocessing, compilation, assembly, and linking.
  • Preprocessing handles lines starting with #, like #include and #define, expanding them before real compilation begins.
  • Compilation translates the preprocessed C code into assembly language, a low-level human-readable representation of machine instructions.
  • Assembly turns assembly code into object code (machine code) stored in .o or .obj files.
  • Linking combines your object code with library code (like printf from the standard library) to produce the final executable.
  • The compiler driver (gcc or clang) runs all these stages for you with one command, but you can stop at any stage with flags like -E, -S, or -c.
  • Understanding the stages helps you read error messages: preprocessing errors mention missing headers, compile errors mention syntax, and linker errors mention missing functions.
  • The linker is what complains about "undefined reference to main" if you forget to write main.
  • This lesson has no code because the topic is the toolchain process, not a C program itself.
Keep in mind
  • Compilation has four stages: preprocess, compile, assemble, link.
  • gcc runs all stages; use -E, -S, -c to stop early and inspect intermediate files.
  • Linker errors mean a function or variable is declared but not defined.
  • Knowing the stages helps you understand error messages and fix problems faster.

Breaking it down

Preprocessing

gcc -E main.c -o main.i

The preprocessor expands #include and #define lines and removes comments. The output is still C code, but with all macros and headers expanded. Errors here usually mean a missing header file.

Compilation

gcc -S main.i -o main.s

The compiler translates the preprocessed C code into assembly language, which is a human-readable form of machine instructions. Syntax errors are caught at this stage.

Assembly

gcc -c main.s -o main.o

The assembler turns assembly code into an object file containing machine code. Object files are not yet runnable because they may reference external functions.

Linking

gcc main.o -o main

The linker combines your object file with library code (like printf) and resolves references. The result is an executable file you can run. Missing function definitions cause linker errors here.

A helpful analogy

Compiling C is like building a house from a blueprint. Preprocessing is gathering materials and reading notes on the blueprint (#include brings in standard parts). Compilation is translating the blueprint into step-by-step instructions for workers. Assembly is cutting the wood into exact pieces. Linking is assembling the pieces and connecting the plumbing (library functions) to make a livable house (executable).

Nesting rules

  • 1.Always compile with warnings enabled: gcc -Wall -Wextra -std=c11 main.c -o main catches many mistakes early.
  • 2.Link order matters: list object files before libraries on the command line, because the linker reads left to right.
  • 3.Never edit object files or executables by hand; always change the source and recompile.

Common mistakes

Confusing compile errors with linker errors

Avoid

Thinking 'undefined reference to printf' is a syntax error

Use instead

Add #include <stdio.h> and ensure you link with the standard library (gcc does this by default)

Undefined reference is a linker error, meaning the function was declared but its code was not found. Syntax errors happen earlier, during compilation.

Forgetting to save the file before compiling

Avoid

Editing main.c in an editor but compiling the old saved version

Use instead

Save (Ctrl+S) before running gcc

The compiler reads the file from disk, not from the editor's memory. Unsaved changes are invisible to it.

Using the wrong file extension

Avoid

gcc main.txt -o main

Use instead

gcc main.c -o main

GCC uses the .c extension to know it is C code. Other extensions may be treated as linker input or ignored, causing confusing errors.

Quick tips

  • •Run gcc -v main.c -o main to see the exact commands the compiler runs at each stage.
  • •Use -save-temps to keep all intermediate files (.i, .s, .o) for inspection.
  • •If you get a linker error, check that you spelled the function name correctly and included the right header.
  • •Compile small programs often; it is easier to find an error in 10 lines than in 100.
  • •Read error messages from top to bottom; the first error often causes a cascade of later ones.
Previous
Next