C++ programs compile in translation units (.cpp files). Headers declare interfaces; one .cpp file provides each non-inline definition the linker needs—similar discipline to multi-file C projects.
Include guards
#ifndef WIDGET_H
#define WIDGET_H
// declarations
#endif
Or #pragma once on supported compilers. The playground uses a single main.cpp, but local projects split headers and sources.
Build pipeline
- Preprocessor expands
#includeand macros - Compiler emits object files per translation unit
- Linker resolves symbols into an executable
Important interview questions and answers
- Q: Undefined reference?
A: Linker error—function declared but not defined, or missing library. - Q: ODR?
A: One Definition Rule—each non-inline function/variable must have exactly one definition across the program.
Self-check
- Why use include guards?
- What does the linker do?
Tip: One Definition Rule—inline functions in headers OK; non-inline function definitions belong in exactly one .cpp file.
Interview prep
- Include guards purpose?
Prevent the same header from being processed twice, avoiding duplicate definitions.
- Undefined reference?
Linker error—function declared but not defined, or required library not linked.