C Multi-File Projects: Directory Architecture & Linking Masterclass
Welcome to Phase 21 (Chapter 58): C Multi-File Projects โ Directory Architecture & Linking Masterclass! Professional C codebases are structured into modular components across separate directories. In this guide, you will master production C folder structures and the two-stage build pipeline.
1. Compilation Stage: `gcc -Iinclude -c src/logger.c -o build/logger.o` converts C source files into independent object files.
2. Linking Stage: `gcc build/*.o -o bin/app` combines object files into a single binary executable.
Q1: What causes an "undefined reference to symbol" linker error?
The function was declared in a header file, but its `.c` implementation file was not compiled or not passed to the linker command line.
Q2: What is the purpose of the -I compiler flag?
The `-Iinclude` flag tells GCC to search the specified `include/` directory when resolving `#include "header.h"` directives.
Q3: Why separate compilation into .o files before linking?
Incremental compilation! When you modify 1 source file in a 1000-file project, only that single `.c` file needs recompilation before relinking.
Q4: What is an Object File (.o / .obj)?
A binary file containing compiled machine code instructions and symbol relocation tables, but without an entry main point or resolved external addresses.
Q5: What is the difference between internal and external linkage?
`static` limits symbol visibility to its own translation unit. `extern` exposes symbols globally to the linker across all translation units.