C 包括后卫

发布于 2024-12-18 06:37:08 字数 222 浏览 1 评论 0原文

file1.c第一次包含inc.h(包含包含保护#ifndef INC_H)时,#define INC_H 被执行。但现在,当另一个 file2.c 包含相同的 inc.h 时,宏 INC_H 是否已定义,所有内容都与之前相同这里没有传播定义?

When file1.c includes inc.h (containing the include guard #ifndef INC_H) for the first time, the #define INC_H is performed. But now, when another file2.c includes the same inc.h, is the macro INC_H already defined, all it's the same story and previous definition is not propagated here?

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

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

发布评论

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

评论(5

一萌ing 2024-12-25 06:37:08

单独的编译之间不会保留宏定义。

The macro definition is not preserved between separate compilations.

通知家属抬走 2024-12-25 06:37:08

但是现在,当另一个file2.c包含相同的inc.h时,宏INC_H是否已经定义了,

是和否。这取决于。

  • 如果file2.c包含一些包含inc.h的标头,则yesINC_H已经为 file2.c 定义。对于任何级别的包含都是如此。

  • 否则,尚未定义。

防护措施可防止标头多次间接或直接包含在文件中!

But now, when another file2.c includes the same inc.h, is the macro INC_H already defined,

Yes and No. It depends.

  • If file2.c includes some header which includes inc.h, then yes, INC_H is already defined for file2.c. This is true for any level of inclusion.

  • Else, no it is not already defined.

Guards prevent header from being included in a file, indirectly or directly, more than once!

北笙凉宸 2024-12-25 06:37:08

当您编译 file2.c 时,编译器会重新启动。编译 file1.c 时定义的任何预处理器符号在编译 file2.c 期间都不起作用。

When you complile file2.c, the compiler starts afresh. Whatever preprocessor symbols got defined when file1.c got compiled play no part during the compilation of file2.c.

那片花海 2024-12-25 06:37:08

定义不会在 *.c 文件之间传播。如果是的话,您一开始就不需要 *.h 文件。 (不过,您可以#include 一个*.c 文件,但那是另一回事了。)

Definitions are not propagated between *.c files. If they were, you would not need *.h files in the first place. (However, you can #include a *.c file, but that is another story.)

濫情▎り 2024-12-25 06:37:08

不,想一下“#include”的作用。它本质上是将头文件的内容复制到包含它的位置。

因此,INC_H 将在 inc.h 第一次包含在 .c 文件中时定义。但是,这对于另一个 .c 文件没有任何改变。

当包含文件包含其他包含内容时,包含防护非常有用。在这些情况下,您可以使用防护装置来避免麻烦。

No, think a moment what "#include" does. It essentially copies the contents of the header file to the place where it is included.

So INC_H will be defined the first time inc.h is included in a .c file. However, this changes nothing for another .c file.

Include guards are useful when include files have other include into it. In these cases you can avoid trouble using the guards.

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