【发布时间】:2019-06-24 11:08:29
【问题描述】:
例如,如果我有以下三个用 C 语言编写的文件:
hello.h
void hello (const char * name);
hello.c
#include "hello.h"
int
main (void)
{
hello ("world");
return 0;
}
hello_fn.c
#include <stdio.h>
#include "hello.h"
void
hello (const char * name)
{
printf ("Hello, %s!\n", name);
}
我知道将#include "hello.h" 行添加到hello.c 告诉编译器hello (const char * name) 的定义将在外部文件(单独编译)期间提供,但是我现在的问题是为什么在 hello_fn.c 包含它自己的定义时将它添加到文件 hello_fn.c hello (const char * name) ,我的意思是它不会从外部提供?
谢谢
【问题讨论】:
-
如果头文件包含其他信息,比如两个源文件都需要的结构或宏怎么办?它还将确保函数定义与声明匹配。
-
@Someprogrammerdude 在这种情况下,您所说的是 hello_fn.c 中的结构,其他功能 ...etc 需要它,但对于相同的功能 void hello(...).. 不需要?那是正确的...我的意思是我有这种误解,因为我认为我正在阅读的书中的作者只是添加了它而没有任何额外的需要,只是为了那个例子?
标签: c gcc include c-preprocessor header-files