[英]Why a header file is added in the source code where the function is ALREADY defined?
For example if I have the following three files written in C : 例如,如果我用C编写了以下三个文件:
hello.h 你好
void hello (const char * name);
hello.c 你好ç
#include "hello.h"
int
main (void)
{
hello ("world");
return 0;
}
hello_fn.c hello_fn.c
#include <stdio.h>
#include "hello.h"
void
hello (const char * name)
{
printf ("Hello, %s!\n", name);
}
I know that adding the line #include "hello.h"
to the hello.c tells the compiler that the definition of hello (const char * name)
will be supplied during an external file (separate compilation) , but my question now why it is added to the file hello_fn.c at the time hello_fn.c contain it self the definition of hello (const char * name)
, I mean it will not be supplied from outside ? 我知道将
#include "hello.h"
到hello.c中告诉编译器在外部文件(单独编译)期间将提供hello (const char * name)
的定义, 但是我现在的问题是为什么在hello_fn.c包含hello (const char * name)
的定义时将其添加到文件hello_fn.c中,我的意思是不会从外部提供它吗?
Thanks 谢谢
This is actually good practice since the compiler can check your declaration (in the header) against your definition (in hello_fn.c
). 这实际上是一个好习惯,因为编译器可以根据您的定义(在
hello_fn.c
)检查您的声明(在标头中)。
Without that, there is no way to ensure that they match. 没有这些,就无法确保它们匹配。 You could quite easily put:
您可以很容易地提出:
void hello (int notCorrect) { ... }
into your C source file and it would happily compile the two source files independently and then (depending on what you actually do in the ...
bit) fail spectacularly at run-time. 放入您的C源文件中,它将很乐意独立地编译两个源文件,然后(取决于您在
...
位中实际执行的操作)在运行时会严重失败。
For example, consider the files: 例如,考虑以下文件:
hello.c:
#include <stdio.h>
#include "hello.h"
int main(void) { hello(42); }
hello_fn.c:
#include <stdio.h>
//#include "hello.h"
void hello(char *x) { puts(x); }
hello.h:
int hello(int x);
This compiles fine because each C source file is internally consistent in what it states. 这样编译就可以了,因为每个C源文件的状态在内部都是一致的。 The
hello.c/hello.h
pair thinks that hello
takes an int
while hello_fn.c
thinks it takes a C string. hello.c/hello.h
对认为hello
采用int
而hello_fn.c
认为采用C字符串。
I get a core dump (1) when running, because the value 42
is not a valid string address. 我在运行时得到核心转储(1) ,因为值
42
不是有效的字符串地址。 When I uncomment the include
line in hello_fn.c
, the compiler complains (rightly) that my declaration and definition disagree (because the hello_fn.c/hello.h
pair is now inconsistent). 当我取消注释
hello_fn.c
的include
hello_fn.c
,编译器会(正确地)抱怨我的声明和定义不同意(因为hello_fn.c/hello.h
对现在不一致)。
On top of that, there may be other stuff in the header (though not in your example case) that exists only in the header. 最重要的是,标头中可能存在仅存在于标头中的其他内容(尽管在您的示例中不是)。 This is typically declarations that are shared amongst caller and callee, such as types,
extern
items, #define
items and so on. 这通常是在调用方和被调用方之间共享的声明,例如类型,
extern
项目,# #define
项目等。 It makes little sense to declare them separately in the header and source file. 在头文件和源文件中分别声明它们几乎没有意义。
(1) Actual results may vary since this is undefined behaviour. (1) 实际结果可能会有所不同,因为这是不确定的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.