简体   繁体   English

标准标题/库中是否包含防护措施?

[英]Are there include guards in the standard headers/libraries?

By the fact if I include stdlib.h into each file of my program and I do not get an re-definition error. 事实上,如果我将stdlib.h包含在程序的每个文件中,并且没有收到重新定义错误。 So, the answer is yes. 因此,答案是肯定的。 Right? 对?

I'm reading the libxml2 source code, and in HTMLparser.c there is this part: 我正在阅读libxml2源代码,并且在HTMLparser.c包含以下部分:

#include <string.h>
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

My question is: Why use include guards here? 我的问题是:为什么在这里使用include guards

Those aren't include guards. 这些不包括警卫。 These macros are set if the corresponding headers are present in the system, resp. 如果系统中存在相应的标头,则设置这些宏。 detected during configuration. 在配置期间检测到。 If they were included unconditionally and they are not present, the compilation would fail. 如果无条件地包含了它们并且它们不存在,则编译将失败。

Yes, there are include guards in system headers. 是的,系统标头中包含保护措施。 Just take a look eg at "string.h": 看看例如“ string.h”:

/* Excerpt from GCC string.h */
/*
 *  ISO C99 Standard: 7.21 String handling  <string.h>
 */

#ifndef _STRING_H
#define _STRING_H   1

The defines for 的定义

HAVE_CTYPE_H
HAVE_STDLIB_H

usually come from a config header which may be generated with a ./configure script which checks for the availability of the header files. 通常来自配置标头,该标头可以通过./configure脚本生成,该脚本检查标头文件的可用性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM