如何一个头文件被编译成可执行文件? [英] How is a header file being compiled into the executable file?

查看:227
本文介绍了如何一个头文件被编译成可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你包含一个头文件(.H,.HPP,.hh,.HXX扩展)从C标准库或C ++ STL(甚至包括微软的大一个像 WINDOWS.H ),并使用从它的功能只有在一定的数量有限,不会对整个文件成为二进制和被复制到可执行文件,或正在使用的只是具体的,有关职能成为二进制<? / p>

如果整个文件被复制(这就是我认为的确是这样,因为 WINDOWS.H 具有宏定义,以减少冗余; WIN32_LEAN_AND_MEAN 例如) - 它为什么会发生这样?为什么没有的#include preprocessor命令只复制使用的功能(和正在在后台使用这些功能之外的功能)?是不是更划算?

如果只有相关职能正在复制 - 为什么我们要做头,包括摆在首位?我们为什么不刚才正在使用大量的功能池,需要在编译时,就像在跨preTED语言如PHP(以及某些情况下在Python)的情况。如果只是因为C和C ++年长已经标准化,为什么不这样做在他们的新版本?或者至少是现代编译器允许它(我们已经看到情况下,编译器弯曲有利于现代开发商的老化语言)?


解决方案

头文件只是告诉编译器是什么类型的外部函数和变量,定义宏,类型等没有被复制。是那些获得在源文件中引用的任何函数和变量(所谓的外部符号)将在链接阶段被链接

如果你在程序中包含stdio.h和用printf,编译器增加了printf的作为对象文件中的未解析的符号,然后链接器将尝试找到一个名为printf函数无论是在目标文件明确你链路,或者在库中它被配置为搜索

由于附近指出,有一个#包括文件和文件的内容复制到源文件之间没有真正的区别。如果包含文件中包含的功能或数据定义(不只是声明),那么这些DO成为你的目标文件的一部分。

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)?

解决方案

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.

这篇关于如何一个头文件被编译成可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆