C 包含守卫究竟是做什么的? [英] What exactly do C include guards do?
问题描述
我有一个关于 C 中包含守卫的问题.我已经阅读了一些内容,但希望能得到一些澄清.
I have a question regarding include guards in C. I've done a bit of reading but would appreciate a little bit of clarification.
假设我有一个带有函数定义的头文件header.h".
Let's say I have a header file "header.h" with a function definition.
#ifndef HEADER_FILE
#define HEADER_FILE
int two(void){
return 2;
}
#endif
这个头文件有一个包含保护.但是,我对#define HEADER_FILE 实际上在做什么感到困惑.假设我要忘记包含保护,完全忽略添加#define HEADER_FILE"对我来说是完全合法的.
This header file has an include guard. However, I'm kind of confused as to what #define HEADER_FILE is actually doing. Let's say I were to forget the include guard, it would have been perfectly legal for me to completely ignore adding '#define HEADER_FILE'.
所以我的问题是:当我们定义 HEADER_FILE 时,我们到底在做什么?我们在定义什么?为什么可以忘记包含保护,在这种情况下我们也可以忘记添加#define HEADER_FILE?
So my question: What exactly are we doing when we define HEADER_FILE? What are we defining? And why is it okay to forget the include guard in which case we can also forgot adding #define HEADER_FILE?
感谢任何帮助!
推荐答案
这是一个预处理宏.
所有这些都是预处理器语法,基本上就是说,如果尚未定义此宏,请定义它并包含 #ifndef
和 #endif
之间的所有代码
All of it is preprocessor syntax, that basically says, if this macro has not already been defined, define it and include all code between the #ifndef
and #endif
它的作用是防止多次包含文件,这可能导致您的代码出现问题.
What it accomplishes is preventing the inclusion of file more than once, which can lead to problems in your code.
你的问题:
为什么可以忘记包含保护,在这种情况下我们也可以忘记添加#define HEADER_FILE?
And why is it okay to forget the include guard in which case we can also forgot adding #define HEADER_FILE?
忘记它也没关系,因为没有它它仍然是合法的 C 代码.如果没有逻辑说明为什么不应该这样做,预处理器会在编译之前处理您的文件,并在最终程序中包含指定的代码.这只是一种常见的做法,但不是必需的.
It's OK to forget it because it's still legal C code without it. The preprocessor processes your file before it's compiled and includes the specified code in your final program if there's no logic specifying why it shouldn't. It's simply a common practice, but it's not required.
一个简单的例子可能有助于说明这是如何工作的:
A simple example might help illustrate how this works:
您的头文件 header_file.h
我们会说,包含以下内容:
Your header file, header_file.h
we'll say, contains this:
#ifndef HEADER_FILE
#define HEADER_FILE
int two(void){
return 2;
}
#endif
在另一个文件 (foo.c
) 中,您可能有:
In another file (foo.c
), you might have:
#include "header_file.h"
void foo() {
int value = two();
printf("foo value=%d
", value);
}
一旦预处理"并准备好编译,这将转化为:
What this will translate to once it's "preprocessed" and ready for compilation is this:
int two(void){
return 2;
}
void foo() {
int value = two();
printf("foo value=%d
", value);
}
所有包含保护在这里完成的是确定是否应该将 #ifndef ...
和 #endif
之间的标头内容粘贴到原来的位置#include
.
All the include guard is accomplishing here is determining whether or not the header contents between the #ifndef ...
and #endif
should be pasted in place of the original #include
.
但是,由于该函数没有声明为 extern
或 static
,并且实际上是在头文件中实现的,因此如果您尝试使用它就会遇到问题在另一个源文件中,因为不会包含函数定义.
However, since that function is not declared extern
or static
, and is actually implemented in a header file, you'd have a problem if you tried to use it in another source file, since the function definition would not be included.
这篇关于C 包含守卫究竟是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!