A solution to the multiple inclusion problem

Marc San Soucie mds at wang.UUCP
Wed Oct 25 09:24:23 AEST 1989


Lawrence Crowl writes:

> Each include file defines a symbol (preferably related to its name).  For
> example, in foo.h:
>         #define foo_h
>         ...
> Each file that includes foo.h, protects the inclusion with a #ifndef:
>         ...
>         #ifndef foo_h
>         #include "foo.h"
>         #endif foo_h
>         ...
> Programmers of include files can further protect against multiple inclusion
> by using the standard mechanism:
>         #ifndef foo_h
>         #define foo_h 
>         ...
>         #endif foo_h


Then when programmers of programs can count on header files which follow such
conventions, top-level programs can use constructs like the second one to
insure that they do not also include extra copies of include files.

This is not an insignificant issue when file opens are slow, such as across
some networked file systems. Admittedly compiling across a network is kind of
goofy, but mature systems shouldn't impose such restrictions on their users.

    Marc San Soucie
    Massachusetts
    mds at wang.wang.com



More information about the Comp.lang.c mailing list