[net.lang.c] duplicate #includes

stanton@fortune.UUCP (W. Dean Stanton) (08/04/84)

Someone suggested "marking" a header file with a

	#define HeaderLoaded 1

so some file that doesn't want to load it twice can check.
I have two further suggestions.

1.	Either the file about to include can check:

		#ifndef		HeaderLoaded
		#   include	"header.h"
		#endif		HeaderLoaded

	or a careful include file can check:

		#ifndef		HeaderLoaded
		#   define	HeaderLoaded
		#    . . . /* stuff to only include once */
		#endif		HeaderLoaded

	(I feel it is probably the include file which should know if
	what it does can be done more than once.)  It is interesting
	to note that some CPPs don't care if the #endif is left off
	a "successful" #if or #ifdef, so be careful to include it.

2.	As long as you are defining something, I make it a general rule
	to prefix my include files with BOTH documentation-type comments,
	including who changed what when, and ALSO a "flag" which gives
	CPP the ability to check the versions if it cares:

		#   define	HeaderVersion	840803

	This could be a string, of course, but the "integer" (assuming such
	a number fits in your CPP's "integer") allows checks.  For example,
	version numbers in two files damn well better match.  Or the code
	better be newer than the header file, or bomb (which I will take
	as an indication that I should check the changes in the header file
	for compatibility).

	Of course, this last trick is most useful if your CPP allows you
	to print error messages at CPP time if some unfriendly set of
	values is discovered.  Does anyone's CPP support this?

				- W. Dean Stanton, Graphics Software
UUCP:	{ihnp4,ucbvax!amd,hpda,sri-unix,harpo}!fortune!stanton
USPS:	Fortune Systems Corp, 101 Twin Dolphin Drive, Redwood City, CA 94065
Phone:	(415) 594-2835