How does a header file get compiled into an executable file?

When you include a header file (.h, .hpp, .hh, .hxx extensions) from the C standard library or C ++ STL (or even include Microsoft's big one, for example windows.h

) and only use a certain limited number of functions from it, the whole does the file become binary and is copied into the executable, or is it only certain, corresponding functions that are being used become binary?

If the entire file is copied (and what I think is actually happening because it windows.h

has macros to reduce redundancy; for example WIN32_LEAN_AND_MEAN

) - why is this happening? Why #include

does the preprocessor command copy only the functions used (and other functions that are used by those functions in the background)? Isn't it cost effective?

If only relevant functions are copied - why do we need to do the header - in the first place? Why don't we just use a huge set of functions that can be used and compiled as needed, such as the situation in interpreted languages ​​like PHP (and some cases in Python). If it's only because "C and C ++ are older and standardized", why not do it in their newer versions? or at least modern compilers that allow this (we've already seen cases where compilers flex the aging language in favor of the modern developer)?

+2


source to share


5 answers


The header file simply tells the compiler what types of external functions and variables are, defines macros, types, etc. Nothing is copied. Any functions and variables (called external symbols) that are referenced in the source file will be linked during the linker phase.

If you include stdio.h and use printf in your program, the compiler will add printf as an "unresolved" character in the object file, and then the linker will try to find the printf function either in the object files that you explicitly reference or in the libraries it is configured to look for ...



As stated alongside, there is no real difference between #including a file and copying the contents of that file back to the original file. If this included file contains function or data definitions (not just declarations), then they become part of your object file.

+3


source


The totality of the title will be compiled (except for the parts that are protected #if

, #ifdef

and so on, to prevent it).

As is usually done in C, the headers contained only declarations, not definitions, except macros. So header compilation basically puts the names into the compiler's symbol table without generating any code.

In C ++, the header often contains actual definitions, especially when templates are used. In this case, the header can contain many inline function definitions along with class definitions and various declarations.



This does not mean that all content ends up in the final executable. The modern linker does very little to combine multiple definitions of the same object / function and exclude parts that are not used.

Type macros WIN32_LEAN_AND_MEAN

were invented primarily to improve compilation times. They usually have little or no effect on the size of the final executable file.

0


source


The header files are not read directly by the compiler, instead they are read by the preprocessor, which basically copies it directly into the directive's location #include

.

Since most of the header files do not contain any real code, just declarations and macros, in fact, none of these header files really fit into the executables. All macros and recursive directives are #include

resolved by the preprocessor. What is left to the compiler are declarations, types -liases (declarations typedef

) and, well, more declarations. The declarations don't actually generate any code, they are only used by the compiler for its internal state when parsing and generating the actual code.

You might want to read the C preprocessor .

0


source


In my experience, header files have almost nothing to do with file size, because almost all OSs have c or C ++ built in, and therefore no need to copy binaries.

The only time headers affect file size is the one you did. WIN32_LEAN_AND_MEAN is used to exclude services that are almost never used in header files.

If you need more information, I also found this article another article on Stack Overflow.

0


source


This link is about writing files: http://www.cs.bu.edu/teaching/cpp/writing-makefiles/ This link describes h. files using: Why does C ++ have header and .cpp files?

0


source







All Articles