Alan Balmer wrote:
> On 22 Apr 2004 18:52:12 GMT, Neil Cerutti <> wrote:
>
>
>>In Rob Pike's style guide he urges the following:
>>
>> Simple rule: include files should never include include
>> files. If instead they state (in comments or implicitly)
>> what files they need to have included first, the problem of
>> deciding which files to include is pushed to the user
>> (programmer) but in a way that's easy to handle and that,
>> by construction, avoids multiple inclusions.
>>
>>I was startled by this guideline, since it's not what I think of
>>as the usual practice, i.e., brute-force idempotency through
>>header guards in the headers.
>
>
> FWIW, I disagree with Rob Pike on this issue. I believe an include
> file should include whatever other headers it itself needs. However,
> it shouldn't include any others. Finding after the first compile that
> some header file needed another header included in front of it is
> legitimate grounds for cursing the author.
>
>>I have three questions about this advice, for those who use a
>>similar style:
>>
>>1. Is following this guideline facile or hassle? I converted a
>>simple two-header program over to this style, making several
>>errors along the way. I clearly don't have experience
>>with it.
>>
>>2. How does a header state implicitly what it requires for
>>headers? I assume he means that you gleen this information by
>>reading the header file, but I want to make sure I'm not missing
>>something interesting.
>>
>>It seems like a good candidate for automation.
>
>
> What's the third question? 
>
Conjecture: The source of Pike's guideline may not
have been addressing C-language per se, but rather
external factors, such as the "make" program.
(Which makes this OT in c.l.c, I guess.)
The earliest versions of "make" only checked for
explicit dependencies as specified in the "makefile".
Thus, if a.c included a.h which included b.h *and*
b.h was not an explicit dependency in the makefile,
then a.c would not get recompiled when b.h changed.
At best, you got a compile-time error. At worst,
you got undefined behavior or a core-dump after
delivery to the customer.
Developers were usually good about finding all the
#include directives in their own code in order to
create the makefile, but hardly ever checked whether
or not there were nested includes within other files.
This /may/ have been the reason for the guidelines
noted above.
More modern versions of "make" do this checking
for you. So if you have those versions, it has been
automated already.
--
"It is impossible to make anything foolproof
because fools are so ingenious"
- A. Bloch