How is a header file being compiled into the executable file?

℡╲_俬逩灬. 提交于 2019-12-12 15:42:14

问题


When you include a header file (.h, .hpp, .hh, .hxx extensions) from the C Standard Library or the C++ STL (or even include a Microsoft's big one like windows.h), and use only a certain limited number of functions from it, does the whole file become binary and be copied to the executable file, or just the specific, relevant functions that are being used become binary?

If the whole file is being copied (and that's what I think actually happens, since windows.h has macro-defines to reduce redundancy; WIN32_LEAN_AND_MEAN for instance) - why does it happen this way? Why doesn't the #include preprocessor-command copy only the used functions (and other functions that are being used by these functions in the background)? Isn't it more cost-effective?

If only the relevant functions are being copied - why should we have to do header-includes in the first place? Why don't we have just a huge pool of functions being used and compiled when needed, like the situation in interpreted languages such as PHP (and some cases in Python). If it's just because "C and C++ are older and has been standardized", why not doing it in their new versions? or at least modern compilers allowing it (we've already seen cases where compilers bend the aging language in favor of the modern developer)?


回答1:


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

If you include stdio.h and use printf in your program, the compiler adds printf as an "unresolved" symbol in the object file, and then the linker will try to find a function called printf either in the object files you explicitly link, or in the libraries it is configured to search.

As stated nearby, there is no real difference between #including a file and copying the contents of that file into your source file. If that included file contains function or data definitions (not just declarations), then these DO become part of your object file.




回答2:


The entirety of the header will be compiled (excepting parts protected with #if, #ifdef, etc., to prevent it).

As things were normally done in C, headers contained only declarations not definitions, except of macros. As such, compiling the header mostly put names into the compiler's symbol table, without producing any code.

In C++, a header often contains actual definitions, especially when templates are in use. In this case, a header may contain many inline function definitions along with class definitions and various declarations.

That doesn't mean that the entire content ends up in the final executable though. A modern linker does quite a bit to merge multiple definitions of the same object/function, and eliminate parts that aren't used.

Macros like WIN32_LEAN_AND_MEAN were invented primarily to improve compile times. They normally have little or no effect on the size of the final executable.




回答3:


The header files are not read directly by the compiler, instead it's read by the preprocessor who basically copy it straight into the place of the #include directive.

As most header file doesn't contain any actual code, just declaration and macros, there's really nothing of such header files that is really put into the executable files. All macros and recursive #include directives are resolved by the preprocessor. What is left to the compiler is declarations, type-aliases (typedef declarations) and, well, more declarations. Declarations does not 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 about the C preprocessor.




回答4:


From my experience, header files have just about nothing to do with the file size, because almost all OS have c or c++ built-in, and thus it would not be necessary to copy the binaries.

The only case where headers have an effect on file size is when it is one you have made. WIN32_LEAN_AND_MEAN is used to exclude services that are almost never used in the header files.

If you would like more information, I also found this other Stack Overflow article.




回答5:


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



来源:https://stackoverflow.com/questions/20019441/how-is-a-header-file-being-compiled-into-the-executable-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!