Lesson 33 / 36

Headers, Multi-File Programs & Compilation

How #include, header guards and the linker fit together.

#include pastes a file in

#include <stdio.h> (angle brackets — system headers) or #include "myheader.h" (quotes — your own files) literally pastes that file's text at that point before compiling.

Header guards prevent double inclusion

If two files both #include the same header, the guard ensures its contents are processed only once, avoiding "redefined" errors.

#ifndef MATH_UTILS_H
#define MATH_UTILS_H

int add(int a, int b);
int sub(int a, int b);

#endif // MATH_UTILS_H

Compile -> object files -> link

Each .c file compiles separately into an .o (object) file; the linker then stitches all the .o files and library code together into one executable.

Compiling multiple files

gcc handles compiling and linking together in one command when given all sources.

gcc main.c math_utils.c -o app
./app