How C Code Runs: Compiling and Linking
Prerequisites: Your first C program
Turning hello.c into ./hello is actually several steps, usually
hidden behind one gcc command:
Preprocessing -> Compiling -> Assembling -> Linking
The preprocessor handles lines starting with # (like
#include) before real compilation even begins — textually pasting
in header file contents and expanding macros. The compiler then
translates the result into machine code, and the linker combines
that with any library code (like printf's actual implementation)
into one final executable.
gcc -E hello.c # stop after preprocessing, show the result
gcc -S hello.c # stop after compiling, show generated assembly
Common mistakes
- Treating a linker error ("undefined reference to
foo") like a compiler error — it means the compiler understood your code fine, but couldn't find the actual implementation of something you called, often because a library wasn't linked in.