A solution to the multiple inclusion problem
Sun Visualization Products
matthew at sunpix.UUCP
Tue Oct 24 23:46:46 AEST 1989
In article <15316 at bloom-beacon.MIT.EDU> tada at athena.mit.edu (Michael J Zehr) writes:
|In article <14240 at well.UUCP> nagle at well.UUCP (John Nagle) writes:
|>[problem of multiple exclusion, generally solved by:]
|> #ifndef XXX
|> #define XXX
|> ...content...
|> #endif
|>This works, but on the second inclusion, the file still has to be read and
|>parsed, at least by the level of processing that reads "#" statements.
|>[slowing compilation, particularly in C++ programs]
|
|>[proposed compiler optimization binding the 'XXX' defined above to the
|>included file and not including it a second time.]
|
|there's another solution you can manage without having to get your
|compiler vendor to make such an extension:
|
|<foo.h:>
|#ifndef FOO
|#define FOO
|#include "foo_real.h"
|#endif
|
|admittedly there's an extra file open for the first time reading
|through, and you still have to open files when #including the second
|time, but it's not as bad as having to read the entire file in
|(particularly if you have some really *large* files). library files
|could be changed to this with relatively little effort and no compiler
|changes, and users' header files for large systems could be changed to
|work faster *today* without having to wait for the vendors to decide to
|do this.
|
|(unless i'm missing something obvious and being really stupid this
|works....)
|
|-michael j zehr
or if your sure all your header files use the:
#ifndef XXX
#define XXX
...content...
#endif
constructs, you could just alter you source code a bit, and do an '#ifndef', '#endif' around
each '#include'. This would require adding two lines for each included file, and no extra files
open.
/*
* Start of source code
*/
#ifndef STDIO.H
#include <stdio.h>
#endif
#ifndef CTYPE.H
#include <ctype.h>
#endif
Now, if only library writers used the '#ifndef', '#endif' construct consistantly.
--
Matthew Lee Stier |
Sun Microsystems --- RTP, NC 27709-3447 | "Wisconsin Escapee"
uucp: sun!mstier or mcnc!rti!sunpix!matthew |
phone: (919) 469-8300 fax: (919) 460-8355 |
More information about the Comp.lang.c
mailing list