Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top