多次包含头文件

发布于 2024-10-13 07:01:24 字数 268 浏览 2 评论 0原文

#include <stdio.h>
#include <stdio.h>

int main ()
{
   printf ("hello world");
   return 0;
}

当我编译它时,编译器不会因包含 stdio.h 两次而给出任何警告/错误。为什么会这样呢? scanfprintf 等函数现在不是声明和定义了两次吗?

提前致谢

#include <stdio.h>
#include <stdio.h>

int main ()
{
   printf ("hello world");
   return 0;
}

when I compile this, the compiler doesn't give any warning/error for including stdio.h twice. Why is it so? Aren't the functions scanf, printf etc. declared and defined twice now?

Thanks, in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

情独悲 2024-10-20 07:01:24

通常,头文件的编写方式类似于以下示例,以防止此问题:

#ifndef MYHEADER
#define MYHEADER

...


#endif

然后,如果包含多次,则第二个实例会跳过内容。

Typically, header files are written similar to the below example to prevent this problem:

#ifndef MYHEADER
#define MYHEADER

...


#endif

Then, if included more than once, then 2nd instance skips the content.

执着的年纪 2024-10-20 07:01:24

除了使用包含防护之外,正如 Mark Tolonen 的回答所指出的,只要声明兼容,多次声明一个函数就没有问题。这完全没问题:

int foo(int, char *);
int foo(int a, char *p);
extern int foo(int x, char y[]);

事实上,由于每个定义也是一个声明,因此每当您“前向声明”在同一文件中声明的函数时,您就将该函数声明了两次。

不可以的是创建一个函数的多个外部定义;但编写良好的头文件不应创建外部定义 - 仅应创建声明。 printfscanf 函数的(单个)定义应位于目标文件中,该文件在构建程序时与程序链接。

In addition to the use of include guards, as pointed out by Mark Tolonen's answer, there is no problem with declaring a function more than once, as long as the declarations are compatible. This is perfectly fine:

int foo(int, char *);
int foo(int a, char *p);
extern int foo(int x, char y[]);

In fact, since every definition is also a declaration, whenever you "forward-declare" a function declared in the same file, you are declaring the function twice.

What is not OK is to create multiple external definitions of a function; but well-written header files should not create external definitions - only declarations. The (single) definition of the printf and scanf functions should be in an object file, which is linked with your program when it is built.

野生奥特曼 2024-10-20 07:01:24

不,头文件通常定义一个标志,然后仅在该标志未定义时才使用 #ifndef 包含自身。

打开一个看看。

No, the header files usually define a flag and then use #ifndef to include themselves only if the flag was undefined.

Open one up and see.

诗化ㄋ丶相逢 2024-10-20 07:01:24

顺便说一句,“#ifndef”技巧适用于其他人使用的标头(如标准标头)。

如果您需要 #ifndef 作为“私有”程序,那么您就做错了。也就是说,您可以而且应该在项目中组织标题,这样它们就不会被多次包含。

这样做有用的原因之一是可以防止您认为已删除的标题再次弹出。

由于您无法控制公共标头的使用方式,因此此技巧对于公共标头来说是合理的。对于私有标头来说,这个技巧是完全没有必要的。

As an aside, doing the "#ifndef" trick is appropriate for headers used by other people (like the standard headers).

If you need the #ifndef for a "private" program, then you are doing it "wrong". That is, you can and should organize headers within a project so they are not included more than once.

One reason that this is helpful is that keeps headers you think you have deleted from popping up again.

Since you can't control how public headers are used, this trick is reasonable for public headers. This trick is completely unnecessary for private headers.

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