A solution to the multiple inclusion problem

Ken Yap ken at cs.rochester.edu
Tue Oct 24 16:09:20 AEST 1989


|(1) An include file protects itself via #ifndef on a symbol that it defines.
|    This causes the file to be read multiple times.

But is this really as inefficient as people think? I tried the
following on a Sun-4/60

% wc grammar0.cc
     932    2944   19700 grammar0.cc
% g++ -I../h -E grammar0.cc | wc
    3728    8219   63497
% time g++ -I../h -E grammar0.cc > /tmp/foo.cc
0.4u 0.3s 0:01 44% 0+208k 0+9io 0pf+0w

Looks pretty insignificant compared to parsing and CG time.

Just to prove that multiple inclusions were attempted

% grep '#' /tmp/foo.cc | sort +2 -3 | uniq -2 -c
   2 # 1 "../h/pg_types.h" 1
   1 # 42 "../h/pg_types.h"
   1 # 1 "/usr/su/lib/g++-include/BitSet.h" 1
   2 # 28 "/usr/su/lib/g++-include/BitSet.h" 2
   1 # 1 "/usr/su/lib/g++-include/File.h" 1
   3 # 27 "/usr/su/lib/g++-include/File.h" 2
   1 # 1 "/usr/su/lib/g++-include/builtin.h" 1
   3 # 48 "/usr/su/lib/g++-include/builtin.h" 2
   1 # 1 "/usr/su/lib/g++-include/math.h" 1
   1 # 126 "/usr/su/lib/g++-include/math.h" 2
   2 # 1 "/usr/su/lib/g++-include/std.h" 1
   1 # 225 "/usr/su/lib/g++-include/std.h"
   2 # 1 "/usr/su/lib/g++-include/stddef.h" 1
   1 # 59 "/usr/su/lib/g++-include/stddef.h"
   1 # 1 "/usr/su/lib/g++-include/stdio.h" 1
   2 # 1 "/usr/su/lib/g++-include/stream.h" 1
   1 # 160 "/usr/su/lib/g++-include/stream.h"
   1 # 27 "/usr/su/lib/g++-include/stream.h" 2
   2 # 1 "/usr/su/lib/g++-include/values.h" 1
   2 # 92 "/usr/su/lib/g++-include/values.h"
   1 # 1 "error.h" 1
   2 # 1 "grammar.h" 1
   2 # 11 "grammar.h" 2
   1 # 127 "grammar.h"
   4 # 13 "grammar.h" 2
   1 # 1 "grammar0.cc"
  10 # 1 "grammar0.cc" 2
   2 # 1 "item.h" 1
   1 # 98 "item.h"
   2 # 1 "option.h" 1
   1 # 40 "option.h"
   1 # 1 "pg.h" 1
   2 # 10 "pg.h" 2
   1 # 1 "pggram.h" 1
   1 # 1 "predict.h" 1
   1 # 9 "predict.h" 2
   1 # 1 "production.h" 1
   3 # 10 "production.h" 2
   5 # 1 "symbol.h" 1
   1 # 10 "symbol.h" 2
   4 # 140 "symbol.h"
   1 # 9 "symbol.h" 2
   4 # 1 "symset.h" 1
   2 # 10 "symset.h" 2
   3 # 84 "symset.h"
   1 # 9 "symset.h" 2
   1 # 1 "symtab.h" 1
   1 # 9 "symtab.h" 2
   3 # 1 "termset.h" 1
   2 # 50 "termset.h"
   1 # 9 "termset.h" 2

Some of the .h files are pretty hefty, as you can see from the size of
the expanded source.

I don't think I will lose any sleep over what cpp is doing.



More information about the Comp.lang.c mailing list