简体   繁体   English

C源文件的好处包括它自己的头文件

[英]What's the benefit for a C source file include its own header file

I understand that if a source file need to reference functions from other file then it needs to include its header file, but I don't understand why the source file include its own header file. 据我所知,如果源文件需要引用其他文件中的函数,那么它需要包含其头文件,但我不明白为什么源文件包含自己的头文件。 Content in header file is simply being copied and pasted into the source file as function declarations in per-processing time. 头文件中的内容只是作为每个处理时间的函数声明被复制并粘贴到源文件中。 For source file who include its own header file, such "declaration" doesn't seem necessary to me, in fact, project still compile and link no problem after remove the header from it's source file, so what's the reason for source file include its own header? 对于包含自己的头文件的源文件,这样的“声明”对我来说似乎没有必要,实际上,项目在从源文件中删除标题后仍然编译并链接没问题,所以源文件的原因是什么包括它自己的头?

The main benefit is having the compiler verify consistency of your header and its implementation. 主要好处是让编译器验证标头及其实现的一致性。 You do it because it is convenient, not because it is required. 你这样做是因为方便,不是因为它是必需的。 It may definitely be possible to get the project to compile and run correctly without such inclusion, but it complicates maintenance of your project in the long run. 绝对可以让项目在没有这种包含的情况下正确编译和运行,但从长远来看,它会使项目的维护变得复杂。

If your file does not include its own header, you can accidentally get in a situation when forward declaration of a function does not match the definition of the function - perhaps because you added or removed a parameter, and forgot to update the header. 如果您的文件不包含自己的标题,则可能会意外地遇到函数的前向声明与函数定义不匹配的情况 - 可能是因为您添加或删除了参数,并且忘记更新标题。 When this happens, the code relying on the function with mismatch would still compile, but the call would result in undefined behavior. 发生这种情况时,依赖于不匹配函数的代码仍然会编译,但调用会导致未定义的行为。 It is much better to have the compiler catch this error, which happens automatically when your source file includes its own header. 让编译器捕获此错误要好得多,当源文件包含自己的头文件时会自动发生此错误。

The header file tells people what the source file can do. 头文件告诉人们源文件可以做什么。

So the source file for the header file needs to know its obligations. 因此头文件的源文件需要知道它的义务。 That is why it is included. 这就是它被包括在内的原因。

Yours seems a borderline case, but an include file can be viewed as a sort of contract between that source file and any other source files that may require those functions. 您的似乎是一个边缘情况,但是包含文件可以被视为该源文件与可能需要这些功能的任何其他源文件之间的一种契约

By writing the "contract" in a header file, you can ensure that the other source files will know how to invoke those functions, or, rather, you will be sure that the compiler will insert the correct code and check its validity at compile time. 通过在头文件中编写“契约”,您可以确保其他源文件知道如何调用这些函数,或者更确切地说,您将确保编译器将插入正确的代码并在编译时检查其有效性。

But what if you then (even inadvertently) changed the function prototype in the corresponding source file? 但是,如果您(甚至无意中)更改了相应源文件中的函数原型,该怎么办?

By including in that file the same header as everyone else, you will be warned at compile time should a change inadvertently "break" the contract. 通过在该文件中包含与其他人相同的标题,如果更改无意中“破坏”合同,您将在编译时被警告。

Update (from @tmlen's comment): even if in this case it does not, an include file may also use declarations and pragmas such as #defines, typedef, enum, struct and inline as well as compiler macros, that would make no sense writing more than once (actually that would be dangerous to write in two different places, lest the copies get out of sync with each other with disastrous results). 更新(来自@tmlen的评论):即使在这种情况下它没有,包含文件也可能使用声明和编译指示,如#define,typedef,enum,struct和inline以及编译器宏,这些都没有意义不止一次(实际上,在两个不同的地方写作是危险的 ,以免副本彼此不同步而带来灾难性后果)。 Some of those (eg a structure padding pragma ) could become bugs difficult to track down. 其中一些(例如结构填充编译指示 )可能成为难以追踪的错误。

It is useful because functions can be declared before they are defined. 它很有用,因为函数可以在定义之前声明。

So it happens that you have the declaration, followed by a call\\invocation, followed by the implementation. 所以碰巧你有声明,然后是一个call \\ _调用,然后是实现。 You don't have to, but you can. 你不必,但你可以。

The header file contains the declarations. 头文件包含声明。 You're free to invoke anytime as long as the prototype matches. 只要原型匹配,您就可以随时调用。 And as long as the compiler finds an implementation before finishing compilation. 只要编译器在完成编译之前找到实现。

Practical example - assume the following files in a project: 实际示例 - 假设项目中包含以下文件:

/* foo.h */
#ifndef FOO_H
#define FOO_H
double foo( int x );
#endif

/* foo.c */
int foo( int x )
{
  ...
}

/* main.c */
#include "foo.h"

int main( void )
{
  double x = foo( 1 );
  ...
}

Note that the declaration in foo.h does not match the definition in foo.c ; 请注意, foo.h中的声明与foo.c中的定义不匹配; the return types are different. 返回类型不同。 main.c calls the foo function assuming it returns a double , according to the declaration in foo.h . 根据foo.h的声明, main.c调用foo函数,假设它返回一个double

foo.c and main.c are compiled separately from each other. foo.cmain.c是彼此分开编译的。 Since main.c calls foo as declared in foo.h , it compiles successfully. 由于main.c调用foo.h声明的foo ,因此编译成功。 Since foo.c does not include foo.h , the compiler is not aware of the type mismatch between the declaration and definition, so it compiles successfully as well. 由于foo.c 包括foo.h ,编译器不知道的声明和定义的类型不匹配的,所以它编译成功也是如此。

When you link the two object files together, the machine code for the function call won't match up with what the machine code for function definition expects. 将两个目标文件链接在一起时,函数调用的机器代码将与函数定义所需的机器代码不匹配。 The function call expects a double value to be returned, but the function definition returns an int . 函数调用期望返回double值,但函数定义返回int This is a problem, especially if the two types aren't the same size. 这是一个问题,特别是如果这两种类型的大小不同。 Best case scenario is you get a garbage result. 最好的情况是你得到一个垃圾结果。

By including foo.h in foo.c , the compiler can catch this mismatch before you run your program. 通过在foo.c包含foo.h ,编译器可以在运行程序之前捕获这种不匹配。

And, as is pointed out in an earlier answer, if foo.h defines any types or constants used by foo.c , then you definitely need to include it. 并且,正如在前面的回答中指出的,如果foo.h定义了foo.c使用的任何类型或常量,那么你肯定需要包含它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM