Lesson 6 of 8
Multi-File Programs - Headers & Separate Compilation
Real C programs are split across multiple `.c` source files and `.h` header files. Each `.c` file is compiled independently to a `.o` object file. The linker combines them into one executable. Knowing what goes in headers (declarations, types, macros, inline functions) vs source files (definitions, global state) is essential for maintainable C projects.
Header Discipline - What Belongs Where
/* ── math_utils.h ── declarations and types, NOT definitions ─ */
#ifndef MATH_UTILS_H /* include guard - prevents double inclusion */
#define MATH_UTILS_H
#include <stddef.h> /* size_t */
/* Type definitions - OK in headers */
typedef struct {
double x, y;
} Point;
/* Function prototypes - OK in headers */
double distance(Point a, Point b);
double dot_product(Point a, Point b);
int is_zero(Point p);
/* Constants - OK in headers */
#define EPSILON 1e-9
/* static inline - OK in headers (one copy per TU) */
static inline double point_len_sq(Point p) {
return p.x * p.x + p.y * p.y;
}
/* NEVER in headers:
- Function definitions (unless static inline)
- Non-extern variable definitions (causes multiple-definition)
- 'using namespace' or similar (no such thing in C, but applies to C++)
*/
#endif /* MATH_UTILS_H */
/* ── math_utils.c ── definitions ─────────────────────────── */
/*
#include "math_utils.h"
#include <math.h> // sqrt
#include <stdlib.h> // abs
double distance(Point a, Point b) {
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx*dx + dy*dy);
}
double dot_product(Point a, Point b) {
return a.x*b.x + a.y*b.y;
}
int is_zero(Point p) {
return p.x*p.x + p.y*p.y < EPSILON * EPSILON;
}
*/
/* ── main.c ───────────────────────────────────────────────── */
/*
#include <stdio.h>
#include "math_utils.h" // uses " " for local headers, < > for system
int main(void) {
Point a = {0.0, 0.0};
Point b = {3.0, 4.0};
printf("distance = %.2f
", distance(a, b)); // 5.0
printf("dot = %.2f
", dot_product(a, b)); // 0.0
return 0;
}
*/
/* ── Build separately ──────────────────────────────────────
gcc -Wall -std=c99 -c math_utils.c -o math_utils.o
gcc -Wall -std=c99 -c main.c -o main.o
gcc math_utils.o main.o -o myapp -lm
Or all in one:
gcc -Wall -std=c99 math_utils.c main.c -o myapp -lm */Common Mistakes
- No include guards - including the same header twice (via transitive includes) without guards causes 'redefinition of ...' errors. Always use `#ifndef / #define / #endif` or `#pragma once`.
- Putting function definitions in headers (not inline or static) - every `.c` file that includes the header gets a copy. The linker sees multiple definitions and fails.
- `#include "file.h"` vs `#include <file.h>` - quotes search the local directory first; angle brackets search the system include path. Use quotes for your own headers, angle brackets for system headers.
Tip
Tip
Practice MultiFile Programs Headers Separate Compilation in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Separate compilation transforms independent source files into object files before linking
Practice Task
Note
Practice Task - (1) Write a working example of MultiFile Programs Headers Separate Compilation from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with MultiFile Programs Headers Separate Compilation is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- Real C programs are split across multiple `.
- No include guards - including the same header twice (via transitive includes) without guards causes 'redefinition of ...' errors. Always use `#ifndef / #define / #endif` or `#pragma once`.
- Putting function definitions in headers (not inline or static) - every `.c` file that includes the header gets a copy. The linker sees multiple definitions and fails.
- `#include "file.h"` vs `#include <file.h>` - quotes search the local directory first; angle brackets search the system include path. Use quotes for your own headers, angle brackets for system headers.
Finished this lesson?