C 中的头文件有什么意义?
可能的重复:
[C] 每个源文件的标头。
在 C++ 中为什么要有头文件和 cpp 文件?
C++ - .h 文件中应包含哪些内容?< /p>
是头文件存在于 C 中的唯一原因
因此开发人员可以快速查看哪些功能可用
他们可以采取什么论据?
还是跟编译器有关系?
为什么没有其他语言使用这种方法?
是我一个人的问题,还是好像有2套函数定义
只会导致更多的维护和更多的出错空间?
或者了解头文件只是每个 C 开发人员都必须知道的事情吗?
Possible Duplicates:
[C] Header per source file.
In C++ why have header files and cpp files?
C++ - What should go into an .h file?
Is the only reason header files exist in C
so a developer can quickly see what functions are available
and what arguments they can take?
Or is it something to do with the compiler?
Why has no other language used this method?
Is it just me, or does it seem that having 2 sets of function definitions
will only lead to more maintenance and more room for errors?
Or is knowing about header files just something every C developer must know?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
需要头文件来声明可用的函数和变量。您可能根本无法访问定义(=.c 文件); C 支持库中代码的仅二进制分发。
Header files are needed to declare functions and variables that are available. You might not have access to the definitions (=the .c files) at all; C supports binary-only distribution of code in libraries.
编译器需要头文件中的信息来了解哪些函数、结构等可用以及如何使用它们。
所有语言都需要此类信息,尽管它们以不同的方式检索信息。例如,Java 编译器通过扫描类文件或 java 源代码来检索信息来完成此操作。
Java 方式的缺点是编译器可能需要在内存中保存更多的信息才能做到这一点。这在今天没什么大不了的,但在七十年代,当 C 语言创建时,根本不可能在内存中保存那么多信息。
The compiler needs the information in the header files to know what functions, structures, etc are available and how to use them.
All languages needs this kind of information, although they retrieve the information in different ways. For example, a Java compiler does this by scanning either the class-file or the java source code to retrieve the information.
The drawback with the Java-way is that the compiler potentially needs to hold a much more of information in its memory to be able to do this. This is no big deal today, but in the seventies, when the C language was created, it was simply not possible to keep that much information in memory.
标头存在的主要原因是在多个源文件之间共享声明。
假设您在文件
ac
中定义了函数float *f(int a, int b)
并在bc
和dc 中重用
。为了允许编译器正确检查参数和返回值,您可以将函数原型放在头文件中并将其包含在.c
源文件中,或者在每个源文件中重复该原型。typedef 等也是如此。
理论上,虽然您可以在每个源文件中重复相同的声明,但正确管理它将成为真正的噩梦。
有些语言使用相同的方法。我记得 TurboPascal 单位并没有太大不同。您可以将
use ...
放在开头,以表明您将需要在其他地方定义的函数。我不记得它是否也被传递到 Delphi 中。The main reason headers exist is to share declarations among multiple source files.
Say you have the function
float *f(int a, int b)
defined in the filea.c
and reused inb.c
andd.c
. To allow the compiler to properly check arguments and return values you either put the function prototype in an header file and include it in the.c
source files or you repeat the prototype in each source file.Same goes for
typedef
etc.While you could, in theory, repeat the same declaration in each source file, it would become a real nightmare to properly manage it.
Some language uses the same approach. I remember the TurboPascal units being not very different. You would put
use ...
at the beginning to signal that you were going to require functions that were defined elsewhere. I can't remember if that was passed into Delphi as well.可能更多。
probably more.