简体   繁体   English

为什么在已定义函数的源代码中添加头文件?

[英]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采用inthello_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.cinclude 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.

相关问题 头文件中的外部变量未正确链接到定义它的源文件 - Extern variable in header file not linked properly to source file where it is defined 我的 header 文件中已定义的对 function 的未定义引用 - Undefined reference to function that is already defined in my header file 如何使用在标头中声明并在函数中的源代码中定义的结构 - How to use structs declared in the header and defined in source code by a function 在哪里可以找到该函数的源代码? - Where to find the source code of this function? 我可以在内核模块中调用在内核源文件的另一个头文件中定义的静态内联函数吗? - In kernel module can I call static inline function defined in another header file of the kernel source? 为什么某些函数声明extern和头文件不包含在Git源代码的源代码中? - Why are some functions declared extern and header file not included in source in Git source code? 为什么在 Visual Studio 代码中不需要 header 文件中的 function 声明? - Why is that with Visual Studio code the function declaration in header file not required? 重新初始化已在其他源文件中定义的全局变量 - reinitialize a global variable that is already defined in other source file 头文件与源文件中的外部函数 - extern function in header file vs source file 为什么C头文件中的函数声明和函数定义具有相同的名称(来自Redis源) - Why can a function declaration and function definition with same name in the C header file (from redis source)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM