سؤال

By the fact if I include stdlib.h into each file of my program and I do not get an re-definition error. So, the answer is yes. Right?

I'm reading the libxml2 source code, and in HTMLparser.c there is this part:

#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?

هل كانت مفيدة؟

المحلول

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 e.g. at "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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top