C包括守卫到底是做什么的? [英] What exactly do C include guards do?

查看:118
本文介绍了C包括守卫到底是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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实际在做什么感到困惑.假设我忘记了include防护,完全忽略添加'#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\n", 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\n", 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.

但是,由于该函数未声明为externstatic,并且实际上是在头文件中实现的,因此,如果尝试在另一个源文件中使用该函数,则会遇到问题,因为该函数定义会不包括在内.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆