A solution to the multiple inclusion problem

Karl Heuer karl at haddock.ima.isc.com
Sat Oct 28 03:59:31 AEST 1989


In article <FOX.89Oct26215145 at novax.allegra.att.com> fox at allegra.att.com (David Fox) writes:
>A problem with the idea of having the compiler recognize the
>	#ifndef FOO  #define FOO  ... #endif
>arrangement (I wouldn't call it a construct) is what if the
>including file explicitly #undefs FOO?

The optimization is that the compiler, on first encountering a header H that
fits the pattern `#if EXPR ... #endif', stores the pair <H, EXPR> in an
internal table.  (In your example, the expression is `!defined(FOO)'.)  Any
request to include a file F first checks the table to see if F appears as the
left side of some pair <F, EXPR>, and if so, interprets the request as if it
had read `#if EXPR   #include F   #endif'.  So if the user has disabled the
guard expression, the file will indeed be included a second time, as desired.

(Note also that it's not necessary for the compiler to be able to determine
that two names denote the same file.  If it fails to detect this, then the
optimization doesn't take place, but the semantics are preserved.)

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list