Search code examples
cwinapicompilationlinkerheader-files

Importing C header files question about what is included


I am a bit confused about header files.

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. For example, if only the MessageBox() function was implemented in the source code, then just the necessary parts would be included from the header file.

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.

It seems like quite a contradiction, so I was hoping someone could help me get my facts straight.


Solution

  • No,

    if you include file the line

    #include <file.h> // or "file.h"
    

    is replaced by the content of the file.h

    Example: https://godbolt.org/z/a3WEP6hdP

    Header files are not libraries or object files. When you link the linker will link only the used function (more precisely all functions from the used segments).

    But would my final executable file size be smaller if I used WindowsHModular? Surely including <windows.h> with all of its hundreds, or maybe thousands, or lines is going to bloat the executable?

    The correct .h file does not define any data or functions. It should contain only macro definitions, data types declarations, extern object declarations and function prototypes. .h file can be hundreds of thousands of lines long but it will not add anything to the executable.