C 包括后卫
当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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
单独的编译之间不会保留宏定义。
The macro definition is not preserved between separate compilations.
是和否。这取决于。
如果
file2.c
包含一些包含inc.h
的标头,则yes,INC_H
已经为file2.c
定义。对于任何级别的包含都是如此。否则,否尚未定义。
防护措施可防止标头多次间接或直接包含在文件中!
Yes and No. It depends.
If
file2.c
includes some header which includesinc.h
, then yes,INC_H
is already defined forfile2.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!
当您编译
file2.c
时,编译器会重新启动。编译file1.c
时定义的任何预处理器符号在编译file2.c
期间都不起作用。When you complile
file2.c
, the compiler starts afresh. Whatever preprocessor symbols got defined whenfile1.c
got compiled play no part during the compilation offile2.c
.定义不会在
*.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.)不,想一下“
#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 timeinc.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.