Compilation Model
Turning C source into a running program happens in four stages: the preprocessor expands text, the compiler translates C into assembly, the assembler produces machine code, and the linker joins the pieces into one executable.
Detailed Explanation
When you type cc prog.c -o prog, one command quietly runs four different programs. Each one consumes a specific kind of input and produces a specific kind of output. Learning the boundaries is the fastest way to stop guessing at build errors, because every error message belongs to exactly one stage.
| Stage | Program | Consumes | Produces |
|---|---|---|---|
| Preprocess | cpp | .c plus every .h it includes | .i, expanded C source |
| Compile | the compiler proper | .i | .s, assembly text |
| Assemble | as | .s | .o, an object file |
| Link | ld | .o files and libraries | an executable or shared library |
Preprocessing is pure text substitution. It copies in headers, expands macros, and deletes the branches that #if rules out. It does not know what a type is. Compilation is where C semantics finally apply: scopes, types, integer conversions, and optimization all live here. Assembly is a mechanical translation of mnemonics into bytes, plus a symbol table recording what this object file defines and what it still needs. Linking resolves those needs against the other object files and libraries.
C splits the work this way for a historical reason that turned out to be a good design. In the early 1970s a machine did not have enough memory to hold a whole program's worth of parsed source at once. The answer was separate compilation: translate each .c file on its own into a .o file, then link the .o files at the end. Change one source file and you recompile one file, not the entire program. That is the property make exists to exploit, and it is what made programs of hundreds of thousands of lines buildable at all.
You reach for the individual stages when you are debugging something the combined command hides. cc -E stops after preprocessing, which is how you find out what a macro actually expanded to or which header really got picked up. cc -S stops after compiling, so you can read the assembly the optimizer produced. cc -c stops after assembling, giving you a .o to link later. With no stop flag, the driver runs everything and deletes the intermediate files.
The model costs you something real. Because the compiler sees one file at a time, it cannot verify that a call in main.c matches the function's definition in area.c. Header files are the workaround: a shared text file of declarations that both sides include, so both agree. Mismatches that headers do not catch surface as link errors, or as undefined behavior at run time. You also lose optimization across file boundaries unless you enable link-time optimization with -flto, which pushes real compilation into the link step.
Code Examples
/* main.c
* Run the stages by hand to see the artifacts:
* cc -E main.c -o main.i preprocessed source (text, often huge)
* cc -S main.i -o main.s assembly (text)
* cc -c main.s -o main.o object file (binary, has unresolved symbols)
* cc main.o area.o -o prog link both objects into an executable
*
* Or in one step, which is what you normally do:
* cc main.c area.c -o prog
*/
#include <stdio.h>
#include "area.h" /* declaration of rectangle_area lives here */
int main(void)
{
double a = rectangle_area(3.0, 4.0);
printf("%.1f\n", a); /* prints: 12.0 */
return 0;
}/* area.h — the shared declaration, so main.c and area.c agree */
#ifndef AREA_H
#define AREA_H
double rectangle_area(double width, double height);
#endif
/* area.c — the definition, compiled into its own object file */
#include "area.h"
double rectangle_area(double width, double height)
{
return width * height;
}Common Gotchas
The wording of an error tells you the stage
`area.h: No such file or directory` is the preprocessor. `implicit declaration of function 'rectangle_area'` is the compiler. `undefined reference to 'rectangle_area'` is the linker, and it means you compiled the file but never handed the object to the link step.
`-c` and a single `-o` do not mix for several inputs
`cc -c a.c b.c -o out.o` is an error, because two objects cannot share one output name. Compile each file separately, or drop `-o` and accept `a.o` and `b.o`.
Editing a header invalidates every translation unit that includes it
the build system has no way to know that unless you generate dependency files with `-MMD -MP`. Without them, a stale object file compiled against the old header can link successfully and then misbehave.
The intermediate files are readable
`.i` and `.s` are ordinary text. When a macro or an inline expansion is behaving strangely, reading them is usually faster than reasoning about it.