Loading lessons...
Multi-File Programs
Multi-File Programs
Big programs split source into .c files and declarations into headers (.h).
Header file: mymath.h
#ifndef MYMATH_H
#define MYMATH_H
int square(int x);
#endif
Main program
#include "mymath.h"
int main() {
printf("%d", square(5));
return 0;
}
Separate .c for the implementation
int square(int x) { return x * x; }
Compile together
gcc main.c mymath.c -o app
Include guard
#ifndef MYMATH_H ... #endif prevents double inclusion.
TL;DR
- Headers hold declarations.
- .c files hold definitions.
- Include guards stop duplication.
- List all .c files when compiling.