C 中的头文件有什么意义?

发布于 2024-08-20 03:53:07 字数 606 浏览 5 评论 0原文

可能的重复:
[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

二智少女猫性小仙女 2024-08-27 03:53:07

需要头文件来声明可用的函数和变量。您可能根本无法访问定义(=.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.

最后的乘客 2024-08-27 03:53:07

编译器需要头文件中的信息来了解哪些函数、结构等可用以及如何使用它们。

所有语言都需要此类信息,尽管它们以不同的方式检索信息。例如,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.

枕梦 2024-08-27 03:53:07

标头存在的主要原因是在多个源文件之间共享声明。

假设您在文件 ac 中定义了函数 float *f(int a, int b) 并在 bcdc 中重用。为了允许编译器正确检查参数和返回值,您可以将函数原型放在头文件中并将其包含在 .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 file a.c and reused in b.c and d.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.

梦里寻她 2024-08-27 03:53:07
  1. 了解图书馆中有哪些内容可供您使用。
  2. 将程序拆分为编译器的小块。同时编译一兆字节的 C 文件将占用比大多数现代硬件所能提供的更多的资源。
  3. 减少编译器负载。为什么要在屏幕显示程序中了解深层数据库引擎?让它只学习现在需要的功能。
  4. 分开私人和公共数据。这种使用并不常见,但您可以在 C 中实现 C++ 使用私有字段的用途:每个 .c 文件包含两个 .h 文件,一个包含私有内容的声明,另一个包含其他文件可能需要的任何内容。由于密封化,命名空间冲突的可能性较小,更安全。
  5. 备用配置。 Makefile 决定使用哪个头文件,并且给定两个不同的头文件,相同的代码可以服务于两个不同的平台。

可能更多。

  1. Know what is in a library at your disposal.
  2. Split the program into bite-size chunks for the compiler. Compiling a megabyte of C files simultaneously will take more resources than most modern hardware can offer.
  3. Reduce compiler load. Why should it know in screen display procedures about deep database engine? Let it learn only of functions it needs now.
  4. Separate private and public data. This use isn't frequent but you may implement in C what C++ uses private fields for: each .c file includes two .h files, one with declarations of private stuff, the other with whatever others may require from the file. Less chance of a namespace conflict, safer due to hermetization.
  5. Alternate configs. Makefile decides which header to use, and the same code may service two different platforms given two different header files.

probably more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文