← C ProgrammingC PROGRAMMING 1.6

Preprocessor and Compilation

Macros, conditional compilation, multi-file builds, and the kernel-style macro patterns that show up in real driver source.

The chapter about everything that happens to C source before the actual compiler sees it, how a project turns into more than one file without falling apart, and the specific macro tricks the kernel leans on constantly.

The six chapters

  • A — Preprocessor Directives. #define, #include, and the rest of the directive set — all pure text substitution before real compilation even starts.
  • B — Conditional Compilation. #ifdef/#ifndef/#if for code that only exists on certain platforms or builds.
  • C — Multi-File Compilation. Header guards, the declaration-vs-definition split, and how separate .c files actually become one program.
  • D — Makefiles. Targets, dependencies, and a build rule set that only rebuilds what actually changed.
  • E — Linking & Libraries. Static vs dynamic linking, and how the linker resolves a function call to an actual address.
  • F — Kernel-Style Macros & GCC Extensions. container_of, ARRAY_SIZE, type-safe min/max, offsetof, flexible array members, likely/unlikely, and the GCC extensions that make them possible.

Where this lands in driver work

Chapter F is basically a direct preview of reading real kernel source — container_of in particular shows up everywhere a callback only gets handed a pointer to one struct member and needs to recover the whole containing struct from it. Conditional compilation (B) is exactly how the same driver source supports multiple SoC variants or build configs without forking the file.

Notes from readers

Comments — via GitHub