C Programming • C17 baseline, standards-specific features labeled
Header Files and Multi-File Programs
Split declarations and implementations across translation units and link their object files together.
A header publishes declarations that other source files need. Each source file is compiled as a translation unit, and the linker combines compatible definitions. Put include guards in headers, include the header that declares a function in its implementation, and define each external function exactly once.
Example
/* math_ops.h */
#ifndef MATH_OPS_H
#define MATH_OPS_H
int add_values(int left, int right);
#endif
/* Build example */
cc -std=c17 -Wall -Wextra -c math_ops.c app.c
cc math_ops.o app.o -o app
What to expect
The declaration is shared through the header; the linker combines the two compiled object files.
Key idea: Headers describe interfaces. Avoid defining ordinary non-inline external functions in headers included by many files.