Path: utzoo!attcan!uunet!husc6!rutgers!rochester!pt.cs.cmu.edu!f.gp.cs.cmu.edu!dld From: dld@f.gp.cs.cmu.edu (David Detlefs) Newsgroups: comp.lang.c++ Subject: Re: Including header files minimally. Message-ID: <3633@pt.cs.cmu.edu> Date: 21 Nov 88 16:15:17 GMT Organization: Carnegie-Mellon University, CS/RI Lines: 80 I got a couple of interesting responses to my last post on this subject, on which I'd like to comment. First, Glenn Fowler quite correctly points out that the simple example >x.h: > extern int VARIABLE; >tst.c: > #include "x.h" > #define VARIABLE abc > #include "x.h" breaks my scheme (which was, briefly, to record what preprocessor variables an include file depends on, and their values at the point of first inclusion, and then only reinclude if those values have changed.) Glenn's point is that there is no way to determine what identifiers are preprocessor variables the first time through; any random #define may make what was a perfectly good identifier into a macro. I see no way to save my plan from this fatal flaw, and hereby abandon it publicly (insert "Taps" here...) (These kind of sick examples point out the essential corruption of cpp 1/2 :-) I abandon it to the extent that it was supposed to do everything right always while requiring no new semantics and minimal performance cost. It still may be a useful heuristic, as stated, to compete with the "insert an #include_once declaration in the header file (or in the includer)" or "tell cpp to just include once." A final note: Larry Campbell protests my classification of the practice of including files multiple times in the same program as "obscure at best," putting forth the following example: >------------------------------message.h------------------------------ >#ifdef GenerateTable >#define MSG(name, string) char name[] = string; >#else >#define MSG(name, string) extern char name[]; >#endif > >MSG(err_you_lose, "you lose big time, pal") >MSG(err_get_real, "get real, pal") >MSG(err_swap_mad, "swap read error, you lose your mind") >---------------------------end of message.h-------------------------- I don't think this is bad usage; I probably didn't make it clear that the practice I thought was bad was the inclusion of the same file multiple times in the same *compilation unit* with different intended results. I assume that you would only need to include message.h once per .c file, with GenerateTable either undefined in all but one of the .c files. Fine, no problem; what I would have object to would be doing this using something like ------------------------------message1.h----------------------------- #ifdef GenerateTable #define MSG(name) char name[] = MSG_STRING; #else #define MSG(name) extern char name[]; #endif ---------------------------end of message1.h-------------------------- ------------------------------message.h------------------------------ #define MSG_STRING "you lose big time, pal" #include MSG(err_you_lose) #define MSG_STRING "get real, pal" #include MSG(err_get_real) #define MSG_STRING "swap read error, you lose your mind" #include MSG(err_swap_mad) ---------------------------end of message.h-------------------------- I trust you will agree that this is pretty obscure (at best)! -- Dave Detlefs Any correlation between my employer's opinion Carnegie-Mellon CS and my own is statistical rather than causal, dld@cs.cmu.edu except in those cases where I have helped to form my employer's opinion. (Null disclaimer.) --