My understanding of header files in C, such as #include <windows.h>, is that only the necessary parts are included based upon whatever functions are used in the program.
I guess it depends on what you mean by "included". Possibly you are confusing header inclusion with linking, which is a completely separate stage later in the compilation process.
At a high level, #include
directives are simple. They direct the compiler to read source code from another file, as if it appeared in the place of the directive. That's it. There is no inherent picking and choosing of different pieces. Consider: how would the compiler know what pieces you need before it processes the rest of the source file?
There's no fundamental difference between header files and "regular" source files, but it has become conventional wisdom and very strong custom that only certain kinds of code will be put into headers, primarily:
- function declarations
- macro definitions
struct
, union
, and enum
type definitions
- external variable declarations
typedef
definitions
These are mostly things that need to be declared identically in multiple independent source files, and putting them in header files both facilitates that and makes maintenance a lot easier when one of them needs to change.
These are also things that do not affect the program if they go unused. For example, the resulting program is not larger or more complex if the source declares functions that it never calls, whether by #include
ing a header or by declaring them directly.
However, I have stumbled upon WindowsHModular on GitHub here which claims to allow the programmer to only include what is required, as the GitHub author has split Windows.h
into various modules.
The problem with Windows.h
is that it is huge and complex. Although that doesn't make a difference to compiled programs, it does make the compiler expend a fair amount of effort. The purpose of splitting Windows.h
into separate modules is to reduce the CPU time and memory required to compile programs by allowing you to omit a bunch of declarations that you didn't need anyway.
You're probably better off ignoring WindowsHModular at this point.