Re: [dev] include files should never include include files?

From: Alexander Surma <>
Date: Sat, 16 Jan 2010 16:51:44 +0100

Of course you did not run into problems. You just wasted your precious
processor time on including and parsing header files which you could
have easily prevented
by following that rule.
You might however run into problems when breaking this rule and no
#ifdef-guards are present.

I'm not sure about C++, but I'd say the rule applys as well. You never
compile a header
on it's own so you should put all the includes into the C files from
the beginning.


On Sat, Jan 16, 2010 at 4:41 PM, Joseph Xu <> wrote:
> A little off topic maybe, hope I'll be forgiven ...
> I'm reading Rob Pike's C programming style guide
> (, and the last rule says:
> ----------------------------------------------------------------------
> 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.  Multiple inclusions
> are a bane of systems programming.  It's not rare to have files included
> five or more times to compile a single C source file.  The Unix
> /usr/include/sys stuff is terrible this way.
>      There's a little dance involving #ifdef's that can prevent a file
> being read twice, but it's usually done wrong in practice - the #ifdef's
> are in the file itself, not the file that includes it.  The result is
> often thousands of needless lines of code passing through the lexical
> analyzer, which is (in good compilers) the most expensive phase.
>      Just follow the simple rule.
> ----------------------------------------------------------------------
> This is a little surprising to me as I'm used to putting includes in
> include files all the time. I do use #ifdef header guards, and I've
> never really had any problems violating this rule. So my first question
> is, has anybody actually ran into problems due to violating this rule?
> And secondly, does this rule apply to C++? For example, if I'm defining
> a class that std::vector members, I ordinarily add a #include<vector> in
> the header.
> Thanks for the advice.
> Joseph
Received on Sat Jan 16 2010 - 15:51:44 UTC

This archive was generated by hypermail 2.2.0 : Sat Jan 16 2010 - 16:00:03 UTC