Header File
Noun · Development
Definitions
Header File is a file in C and C++ (typically with a .h or .hpp extension) that contains declarations of functions, classes, types, constants, and macros that are shared across multiple source files. Header files enable separate compilation by allowing source files to know the signatures and types defined elsewhere without seeing the full implementation. When a source file includes a header with #include, the preprocessor copies the header content into the source file before compilation. The linker later resolves the declared symbols to their actual implementations. Common header files like stdio.h, stdlib.h, and string.h provide access to the C standard library. Proper header design uses include guards (#ifndef/#define/#endif) or #pragma once to prevent multiple inclusion. The header/implementation split has been a defining characteristic of C and C++ development, though C++20 modules aim to modernize this system by replacing textual inclusion with a proper module system that improves compilation speed and encapsulation.
In plain English: A file that describes what functions and types exist without including the actual code, so different parts of a program can know about each other during compilation.
Example: "The circular include dependency between these two header files has been haunting us for three sprints."
Etymology
- 1972
- C's compilation model uses #include to bring in header files containing function declarations and type definitions.
- 1980s
- Header files become the standard interface description mechanism in C and C++. 'Include hell' and circular dependencies frustrate developers.
- 1998
- Precompiled headers are introduced to speed up C++ compilation by caching parsed header contents.
- 2020s
- C++20 modules aim to replace header files for modern C++ codebases, addressing long-standing compilation speed and dependency issues.
Origin Story
The File That Tells the Compiler What to Expect
A header file is a source file containing declarations (function signatures, type definitions, macros, and constants) that can be shared across multiple source files in languages like C and C++. The concept emerged from the C programming language's development at Bell Labs in the early 1970s. Dennis Ritchie and Brian Kernighan designed C's compilation model so that each source file (.c) was compiled independently. When one file needed to call a function defined in another, it needed to know the function's signature. Rather than repeating declarations manually, programmers could write them once in a header file (.h) and include it wherever needed using the #include preprocessor directive. The convention of using .h extensions for header files was established in early Unix development. The C standard library headers like stdio.h, stdlib.h, and string.h became some of the most included files in computing history. Header files serve as a contract between the implementation (in the .c file) and its consumers, documenting the public interface without exposing implementation details. C++ inherited and extended the system, though the language eventually introduced modules (standardized in C++20) as a modern alternative that addresses header files' compilation speed and dependency management problems.
Context: C programming language, Bell Labs, early 1970s
Fun fact: The '#include' directive literally copies the entire header file into the source file before compilation. In large C++ projects, a single source file can end up including hundreds of thousands of lines of header code, which is why C++ compilation is notoriously slow.