[comp.lang.c] conditional includes

joe@modcomp.UUCP (01/15/89)

Peter da Silva's discussion of the following construct [slightly modified]:

  main.c
  	...
	#include "windows.h"
	#include "menus.h"
  	...

  windows.h:				menus.h
  	...					...
  	#ifndef GRAPHICS_H			#ifndef GRAPHICS.H
  	#include "graphics.h"			#include "graphics.h"
	#define GRAPHICS_H			#define GRAPHICS.H
  	#endif					#endif
  	...					...

reminded me of a feature that I've always wanted in cpp: the ability to do
conditional includes in a way less clumsy than the above examples.
Specifically, I would like cpp to remember which files have already been
included, and to automatically NOP any extra requests.  Of course, a
conditional include service should not be called "#include".  Perhaps
"#cinclude"?

The idea is obvious enough that it probably has occurred to others.  Have
any of you run across an implementation?  What problems have come up with
it?  Or do you consider it trivial and thus would just be another instance
of "creeping featurism" in the language?  Your thoughts would be appreciated.

joe korty
uunet!modcomp!joe

PS: Send email if you like; I'll be glad to correlate and summarize the
responses.

bill@twwells.uucp (T. William Wells) (01/18/89)

In article <8000011@modcomp> joe@modcomp.UUCP writes:
: The idea is obvious enough that it probably has occurred to others.  Have
: any of you run across an implementation?  What problems have come up with
: it?

Assert.h and anything similar, unless a different keyword is used for
the include.

There is also the little gotcha of identity: should foo and ./foo be
considered the same file? How about on a non-Unix system?

:      Or do you consider it trivial and thus would just be another instance
: of "creeping featurism" in the language?

Yes, it's a creeping feature. The feature is easily implementable in
the existing language and recognizable at a glance as what it is.

---
Bill
{ uunet!proxftl | novavax } !twwells!bill

gwyn@smoke.BRL.MIL (Doug Gwyn ) (01/20/89)

In article <306@lakesys.UUCP> chad@lakesys.UUCP (D. Chadwick Gibbons) writes:
>In article <334@twwells.uucp> bill@twwells.UUCP (T. William Wells) writes:
>|There is also the little gotcha of identity: should foo and ./foo be
>|considered the same file? How about on a non-Unix system?
>	Actually, I would think they are considered the same file.  Yes,
>	even on non UNIX systems.

Nope.  There are systems where "./foo" is a valid filename but has
nothing to do with a file named "foo" and nothing to do with directories.

Systems that conform to IEEE Std 1003.1 (POSIX) will assign the usual
UNIX meaning to the "./" part of such a filename, and others may at
their discretion, but not all C environments are POSIX environments.

joe@modcomp.UUCP (01/21/89)

Hi, fellow netlanders,

Several of you responded to my query about what C compilers, if any, provide
their users with a convenient conditional include facility.  As promised,
I'm summarizing (read: liberally plagiarizing) your letters in this response.

1.  The Objective-C language (used on the NeXT) has exactly the feature
    described.  The keyword used is "#import".

2.  The "High C" compiler for the IBM PC/RT supports once-only includes.  It 
    prevents multiple includes based on filename, so referencing a file by 
    different names will result in multiple inclusion.

3.  One compiler (unnamed) has a cpp pragma "#pragma idempotent" that is
    inserted into the header file.  Any subsequent include of that filename
    is skipped.

4.  A combination of (3) and the original solution results in an effective
    solution which does not require any language extensions:  replace
    "#pragma idempotent" with the lines:

	#ifdef <whatever>
	#define <whatever>

    and add a "#endif" to the end of the include file.  Controlling multiple
    inclusion from within the file itself solves the problem mentioned in (2)
    as well as the excessive wordiness of the original solution.

I have assumed that all respondents prefer to remain anonymous.  My
apologies if that assumption is wrong.

joe korty
uunet!modcomp!joe