Why Multiple Files?
As projects grow, a single source file becomes unmanageable. Splitting code into multiple files enables:
- Modularity — each file handles one concern
- Faster builds — only recompile changed files
- Team collaboration — developers work on separate files
Project Structure
text
Separate Compilation
bash
Makefiles
Automate the build process:
makefile
bash
The extern Keyword
Use
extern to access a global variable defined in another file:c
Static Functions and Variables
static at file scope limits the symbol to that file — it's invisible to the linker:c
Summary
| Concept | Purpose |
|---|---|
| Header files | Share declarations across files |
| Separate compilation | Compile each .c independently |
| Linking | Combine .o files into one binary |
extern | Access globals from other files |
static (file scope) | Hide symbols from other files |
| Makefile | Automate build process |