    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: More Include File Rules</title>
        <link>https://members.accu.org/index.php/articles/470</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Design of applications and programs + Overload Journal #40 - Dec 2000</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c67/">Design</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c164/">40</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c67-164/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c67+164/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;More Include File Rules</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 December 2000 16:46:04 +00:00 or Tue, 26 December 2000 16:46:04 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>Last issue I presented 15 rules-of thumb, which I keep in mind
when working with include files. To be honest, I do not think of
these as rules when I code, I just keep to them, only documenting
them for Overload has forced me to codify them as 15 rules.</p>
<p>I am very keen that people do not follow these rules slavishly.
That is my big problem with coding standards, I do not want to see
rules people follow without thinking; I want people to understand
what they do. Sometimes, it is important to break from the rules.
That is why I was very happy that Kevlin's piece appeared in the
same issue. Hopefully, between us we have got people thinking about
include files.</p>
<p>To complete what I started, I would like to present a few more
rules and discuss some common problems and debugging techniques
with include files.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e26" id=
"d0e26"></a>Clarification</h2>
</div>
<p>Before I get started I must apologise for a potential error in
my last article. In rules 2 and 3 I said that only if a class was
declared as a reference or pointer could its include be replaced
with a forward declaration. While reading Herb Sutter's Exception
C++ I came across his remarks on include file dependencies and he
correctly points out that as long as you are only mentioning a
class name and not actually using it you may replace an include
with a forward declaration.</p>
<p>To clarify my point, suppose we have class <tt class=
"classname">Widget</tt> and class <tt class=
"classname">Thingy</tt>, if Thingy contains a <tt class=
"classname">Widget</tt>, or derives from a <tt class=
"classname">Widget</tt> it must include the full declaration of
<tt class="classname">Widget</tt>. However, if <tt class=
"classname">Thingy</tt> only mentions <tt class=
"classname">Widget</tt> in a method (e.g. <tt class=
"literal">Action(Widget)</tt>, <tt class="literal">Action(const
Widget&amp;)</tt> or <tt class="literal">Action(Widget*))</tt>, or
contains a reference or pointer to <tt class=
"classname">Widget</tt>, then, it does not need to include
<tt class="classname">Widget</tt>'s declaration.</p>
<p>What I was trying to say was: if <tt class=
"classname">Thingy</tt> must access a <tt class=
"classname">Widget</tt>, prefer a pointer or reference to a
<tt class="classname">Widget</tt> rather than containing one. While
this is a valid enough rule in isolation, in practice whether
<tt class="classname">Thingy</tt> uses reference or value semantics
for its <tt class="classname">Widget</tt>s is probably a larger
question.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e86" id="d0e86"></a>The rules</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e89" id="d0e89"></a>Where are the
files?</h3>
</div>
<p><span class="bold"><b>Rule 16:</b></span> Avoid paths in
<tt class="literal">#include</tt></p>
<p>File locations vary platform to platform: is <tt class=
"filename">socket.h</tt> in <tt class="literal">include/</tt> or
<tt class="literal">include/sys/</tt> ? As a general rule it is
wise to avoid include paths inside the source code. By placing part
of the include path in the source code you are tying the physical
locations of source files into the logical source code. If you
choose to change your directory structure at a later date you will
need to change your source file, which impacts reusablability: not
only must the re-user have a copy of your file but they must follow
your directory structure.</p>
<p>Any time you reference the directory layout of the system you
are coupling your code to the physical directory structure, this is
especially true when the parent directory is referenced: e.g. code
such as <tt class="literal">#include &quot;../../utils/helper.h&quot;</tt>
imposes a lot of directory structure on a project.</p>
<p>Include paths specified outside of the code, through
configuration setting, command line options or environment
variables break the dependency between file location and file
content - it is sensible to use them. On occasions it is necessary
to specify part of an include path (e.g. <tt class=
"filename">socket.h</tt>) but this should not be more than two
levels and certainly should not reference the parent directory.
(Microsoft Visual C++ 4.0 contained a bug which caused it to
corrupt make-files which included '.' paths.)</p>
<p><span class="bold"><b>Rule 17:</b></span> Break rule 16 when
using namespaces</p>
<p>Having said all the above I have been thinking about Kevlin's
suggestion that <tt class="literal">namespace</tt>s be placed in a
sub-directory and I think he is right. Classically, a project would
place most of its headers in a few directories: the system headers,
the compiler's headers, third party libraries and one application
include. Inevitable the application directory becomes crowded. Now
that namespaces provide a way of subdividing the application
modules I think echoing these modules in subdirectories is a good
idea.</p>
<p>When a system is divided into modules, e.g. <tt class=
"literal">ProjectX</tt> is divided into gui and xml modules, it
makes sense to sub-divide the interfaces (header files) into their
own spaces, <tt class="literal">ProjectX/include/gui</tt> and
<tt class="literal">ProjectX/include/xml</tt> rather than dump them
all in one directory such as <tt class=
"literal">ProjectX/include</tt>.</p>
<p>Once we have done this we have a choice of either</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>adding all the subdirectories to our include path, hence making
it long and complicated, or,</p>
</li>
<li>
<p>placing <tt class="literal">ProjectX/include</tt> in the path
and prefixing the filename with the module name, e.g. <tt class=
"literal">#inlcude &lt;xml/somefile.h&gt;</tt></p>
</li>
</ol>
</div>
<p>Using option 1, if we where to add a new module we would need to
change the include path and perhaps effect the entire system.
Option 2 follows the both the &quot;least modification&quot; and &quot;only add
code&quot; rules, because no global change would be needed and only the
files which used the new module would be effected.</p>
<p><span class="bold"><b>Rule 18:</b></span> Know the difference
between local and remote includes</p>
<p>The <tt class="literal">#include</tt> is really two directives:
<tt class="literal">#include &quot;foo.h&quot;</tt> and <tt class=
"literal">#include &lt;bar.h&gt;</tt>: local and remote includes.
Traditionally, local meant: search for includes in the same
directory as the current compilation unit, i.e. &quot;local is here&quot;;
remote meant: search for includes from some include-path - but all
this is implementation defined (see K&amp;R or Hatton.)</p>
<p>This could cause problems where the <span class=
"emphasis"><em>current compilation unit</em></span> (say <tt class=
"filename">bar.cpp</tt>) included another file remotely. (say
<tt class="filename">&lt;bar.h&gt;</tt>) which itself, included a
file locally (say,. <tt class="filename">&quot;foo.h&quot;</tt>). Two
problems would occur:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Where <tt class="filename">foo.h</tt> exists in the directory
containing <tt class="filename">bar.h</tt> but not the one
containing <tt class="filename">bar.cpp</tt> : developers can see
the file, but the compiler cannot! Hours of frustration.</p>
</li>
<li>
<p>Two different <tt class="filename">foo.h</tt>'s existed: one in
the same directory as <tt class="filename">bar.cpp</tt> and one in
the same directory as <tt class="filename">foo.h</tt>; naturally,
one works while one does not. Again, hours of frustration!</p>
</li>
</ul>
</div>
<p>To complicate things further, not only is this compiler
dependent, but all compilers allow multiple ways of setting include
paths, e.g. Microsoft allows &quot;additional include directories&quot; as
part of the pre-processor, set for the project (stored in the
<tt class="filename">.DSP</tt> file) while &quot;include directories&quot; is
set in options/tools and is the same for all project on this
machine whether you are compiling the Nuclear Reactor control
project, or you Lottery Random number generator.</p>
<p>Some things, like <tt class="filename">stdio.h</tt> are part of
the compiler environment regardless of project, some things are
always part of the project and some things depend: imagine two
versions of the STL on your system: a small fast version (perfect
for choosing lottery numbers) and a slow but bug-free version
(perfect for fault-tolerant systems).</p>
<p>At some point in your system, the logic in the file must meet
the physical file system and directory layout (a subject worthy of
an article in its own right.). I have always viewed the glue that
bound the two together to be the make-file. The make-file knows how
files fit together with each other and the standard gubbins.</p>
<p>My view on include files was very clear:</p>
<p><span class="bold"><b>Rule 18:</b></span> only use &quot;local
include&quot; where the file is local to the includer, chances are it
will never been seen by anything else,</p>
<p><span class="bold"><b>Rule 19:</b></span> use &lt;remote
include&gt; for everything else, let the make-file ensure the right
thing is seen.</p>
<p>In this world, the Microsoft &quot;additional includes&quot; are part of
the <tt class="filename">makefile</tt> and make sense, but they use
the &quot;local&quot; mechanism.</p>
<p>I cannot give you a solid rule here: in my mind the jury is
still out. Given a blank sheet of paper I</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>use &quot;local&quot; includes for files which are in the same directory
and only visible within this module,</p>
</li>
<li>
<p>use &lt;remote&gt; includes for files which form part of the
interface of this or another module, and are contained in a
separate directory</p>
</li>
<li>
<p>use the environment include paths for system include files, e.g.
<tt class="filename">stdio.h</tt> if I am using a standard library
supplied with the compiler I would list them here</p>
</li>
<li>
<p>use project include paths for application files and third party
files including a third party standard library</p>
</li>
<li>
<p>pay special attention to the standard library include paths: I
do not want Plauger's <tt class="literal">string</tt> and Rogue
Wave's <tt class="literal">list</tt>.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e266" id="d0e266"></a>Common
problems and debugging techniques</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e269" id="d0e269"></a>Porting</h3>
</div>
<p>As mentioned above, the search for header files is
implementation dependent. For example, looking for <tt class=
"literal">#include &quot;fred.h&quot;</tt> Microsoft C++ (5.0) searches:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Directory containing the source file</p>
</li>
<li>
<p>Directory containing the file that included the source file, and
subsequent searches all the way up the include tree These two rules
are skipped for <tt class="literal">#include &lt;fred.h&gt;</tt>,
rules 3 and 4 apply to both types of include:</p>
</li>
<li>
<p>Any directory specified with <tt class="literal">/I</tt> on the
command line</p>
</li>
<li>
<p>Any directory specified with the INCLUDE environment variable:
this corresponds to the options directory list in Developer
Studio.</p>
</li>
</ol>
</div>
<p>While Sun's C++ (4.2) compiler searches:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>The directory containing the source file if the include is
&quot;local&quot; Rule 1 does not apply for &lt;includes&gt; but the
following rules are common:</p>
</li>
<li>
<p>Any directory listed with the <tt class="literal">-I</tt> option
on the command line</p>
</li>
<li>
<p>Standard directory for C++ header files; on my 4.2 compiler this
is: <tt class="literal">/opt/SUNWspro/SC4.2/include/CC</tt></p>
</li>
<li>
<p>Standard directory for C header files; again, on my system this
is: <tt class="literal">/opt/SUNWspro/SC4.2/include/cc</tt></p>
</li>
<li>
<p>In <tt class="literal">/usr/include</tt></p>
</li>
</ol>
</div>
<p>Obviously very different from Microsoft, however, GNU C++
(actually egcs 1.1.2 in this case) complicates things further with
a multitude of extra options, e.g. two search paths are allowed.
Generally, GCC seems closer to Sun but the hard coded paths can be
varied at compile time so the same version may actually search in
different places.</p>
<p>While I always knew differences existed only when researching
this article did I realise how different they can be. Microsoft's
upward-recursion algorithm seems particularly dangerous.</p>
<p>Microsoft's compiler offers nothing to help debug include
problems, Sun provides an -H option which will produce a list of
include files as they are included making it possible to follow the
logic. If memory serves GCC/egcs lists which files included which
file when some compilation errors occur.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e329" id="d0e329"></a>But it is
included!</h3>
</div>
<p>One of the more infuriating problems is when you can see
something is included but the compiler will not recognise it. For
example:</p>
<pre class="programlisting">
// file: fred.h
class Fred { ... }
// file: Flintstones.cpp
#include &lt;willma.h&gt;
#include &lt;fred.h&gt;
Fred fred; // error: undeclared identifier
</pre>
<p>The most common reasons for this are:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>The pre-processor actually found a different file called
<tt class="filename">fred.h</tt> which does not define <tt class=
"classname">Fred</tt>: e.g. you are using Microsoft pre-compiled
headers and the Visual C++ Wizards, you will have multiple copies
of <tt class="filename">stdafx.h</tt> around, something was added
to one but it is not the one included here</p>
</li>
<li>
<p>Macro-guards mixed up: typical when cut-and-paste programming is
used, e.g. you wrote <tt class="filename">willma.h</tt> and knowing
that <tt class="filename">fred.h</tt> was going to be very similar
you pasted the code from <tt class="filename">willma.h</tt> into a
blank file called <tt class="filename">fred.h</tt> and just made
the changes. However, if you forgot to change the <tt class=
"literal">#if defined WILMA_H</tt> at the start of the file
<tt class="filename">fred.h</tt> and hence the pre-processor
ignores the contents of <tt class="filename">fred.h</tt>.</p>
</li>
</ul>
</div>
<p>The quickest way to find out what is happening to your file is
to add a message which identifies it at compile time, e.g.</p>
<pre class="programlisting">
#pragma message(&quot;This is fred.h&quot;)
</pre>
<p>These can be sprinkled around liberally, but remember you will
want to remove them before check in. The best places to put these
are:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Inside the file with the problem: before actually including
<tt class="filename">fred.h</tt>, e.g. <tt class=
"literal">message(&quot;About to include fred.h...&quot;)</tt> or immediately
after including <tt class="filename">fred.h</tt>, e.g. <tt class=
"literal">message(&quot;... fred.h included&quot;)</tt></p>
</li>
<li>
<p>Inside the offending file: before the include guard (&quot;<tt class=
"literal">This is fred.h</tt>&quot;) and inside the include guard
(&quot;<tt class="literal">Inside fred.h include guard</tt>&quot;).</p>
</li>
</ul>
</div>
<p>If you place a message inside the include guard remember that
you should only see it once for each compilation unit, e.g. each
<tt class="filename">.cpp</tt> file, but you will most likely see
it multiple times during a complete build because multiple
compilation units will probably include the file.</p>
<p>In practice the <tt class="literal">pragma message</tt> is the
most powerful tool for debugging header file issues for most
problems. If your compiler does not support <tt class=
"literal">pragma message</tt>, try <tt class="literal">#error</tt>.
Or, add an actual syntax error to the file, these are seldom fatal
to the compile but will show you what is happening</p>
<p>Variations on the &quot;it is included&quot; theme are:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>The file has changed: one of your fellow programmers has revised
<tt class="filename">Fred.h</tt> to declare class <tt class=
"classname">Fred_Impl</tt> instead of <tt class=
"classname">Fred</tt>.</p>
</li>
<li>
<p>Namespaces: these give us a whole new way to hide
identifiers.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e439" id="d0e439"></a>Cannot open
include file: 'barny.h': No such file or directory</h2>
</div>
<p>Failure to actually find a file is perhaps the most common
error. However, it usually has but one course: include paths are
wrong. Sometimes only legwork will pay here. Check each any every
path individually:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>If your using Microsoft DSP files, the Settings/Additional
include paths dialog box is simply too small to be sure you are not
seeing a '1' (one) instead of an 'l' (el), or a '0' (zero) instead
of an 'O'. Copy the text and paste it into a bigger window, break
the line up and check you can <span><b class=
"command">dir</b></span> each directory at the command line.</p>
</li>
<li>
<p>Check your debug and release settings are the same, are you are
building the one you think your building? In the heat of battle it
is not known to be looking at the Debug settings when you actually
have an error in the Release build</p>
</li>
<li>
<p>If your using <tt class="filename">makefile</tt> these can get
quite complex with files including files which include files which
all add to compiler and linker options. Look at the command line
make is issuing and check the paths.</p>
</li>
<li>
<p>If you rely on an INCLUDE environment variable check the shell.
You are executing in actually has it set to what you think it is.
If you have su'ed to a different user to build it, it may have been
reset.</p>
</li>
<li>
<p>Is your compiler looking for headers in the current directory?
(i.e. where the makefile file is) or is it looking in the directory
containing the directory containing the source file?. Or for that
matter, are you looking in the current directory when you should be
looking in the source directory?</p>
</li>
</ul>
</div>
<p>Sometimes there are other courses to this error:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Your system has run out of file handles; unlikely for NT but
some people still develop for MSDOS and even UNIX has limits.</p>
</li>
<li>
<p>Access writes deny access to the file: can you cat the file at
the command line? If you get an access denied the compiler will
too.</p>
</li>
</ul>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e475" id="d0e475"></a>type
redefinition</h3>
</div>
<p>One common error is the type redefinition. This happens when you
declare something, e.g. <tt class="classname">class Table</tt>, and
something you have previously included declares <tt class=
"literal">typedef... Table</tt>. Hopefully my rules on include file
order should help avoid this. However, this is not always
possible.</p>
<p>Developers do not often make this mistake in their own code, but
third party code, be it OS headers, bought in libraries, or in
house libraries usually contain the other copy. (Hopefully, with
namespaces this kind of thing will become less frequent.)</p>
<p>If your lucky you can place your cursor on the offending
definition, hit F1 and the on-line help will tell you about the OS
type of the name - at which point you must give in gracefully and
rename yours. Unfortunately, much that is defined in OS headers is
not documented, at which point grepping the code may help.</p>
<p>A quicker way to find the first declaration is too cheat. Add a
quick declaration before all your includes, something obscure like:
<tt class="literal">typedef short &lt;Offending
identifier&gt;</tt>, and then recompile. Normally this will give
you the same error, but this time on the first declaration. Once
you have removed your extra declaration you will know which items
conflict and resolve the conflict.</p>
<p>A particularly nasty form of this involves macros with the same
name as types or members, e.g. Sun defines a macro called
<tt class="literal">minor</tt> in <tt class=
"filename">sysmacros.h</tt>, once this file has been included
somewhere you will have problems compiling code with CORBA
exceptions as these define a member called <tt class=
"varname">minor</tt>.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e506" id="d0e506"></a>unexpected end
of file found: and other very strange errors</h3>
</div>
<p>As I said right at the start of these articles, including a
header file is very primitive. If you write:</p>
<pre class="programlisting">
// file: fred.h
class Fred {
  ...
// &lt;eof&gt;
// file: flintstones.h
#include &lt;fred.h&gt;
  ...
// &lt;eof&gt;
</pre>
<p>You will at least get an unexpected end of file error, and
possibly a lot of other strange syntax errors. It is obvious here
but it is easy to miss because the file it shows up in, in this
case, <tt class="filename">flintstones.h</tt>, is a long way from
<tt class="filename">fred.h</tt>. Potentially, <tt class=
"filename">flintstones.h</tt> did not include <tt class=
"filename">fred.h</tt> directly, but included <tt class=
"filename">family.h</tt>, which included &quot;<tt class=
"filename">characters.h</tt>&quot;.</p>
<p>Again cut and paste programming is a common cause of this error;
legwork, and a few <tt class="literal">pragma message</tt>s are the
best attack.</p>
<p>One short cut which you can make is to comment out everything
(<tt class="literal">#include</tt>s and source code) in the
<tt class="filename">.cpp</tt> file and re-introduce the includes
one at a time until the error appears. Check the file you just
included, if you are lucky it is guilty, if not repeat the process
with its include's too.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e546" id=
"d0e546"></a>Dependencies</h2>
</div>
<p>The includes of the system map out the dependencies of the
system, which files depend on which, which subsystem depends on
which. This is one third of your physical design - another third is
the library dependencies and the final third your directory
structure and make-files (which are the most physical part of your
system.)</p>
<p>When designing a system I find it useful to map out the
dependencies before hand, find out which parts will depend on
which, which are the low level parts of the system, which the high,
and which are the same level but independent.</p>
<p>If your dependency strategy is clear, your include files will be
clear and you should have no problem following rules such as &quot;most
inflexible first, most flexible last&quot;. If you find this is not
clear you have a potential problem.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e555" id="d0e555"></a>Final
words...</h2>
</div>
<p>I hope the fact that Kevlin and myself have managed to write
around 10,000 words on the subject of include files convinces you
that there is more here than meets the eye. The fact is, include
files are the first point at which your logical design meets the
physical build environment and therefore determines much of what is
to come.</p>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e560" id="d0e560"></a>References</h2>
</div>
<div class="bibliomixed">
<p class="bibliomixed">&quot;C++ Patterns - Source Cohesion and
Decoupling&quot;, Kevlin Henney, <span class="citetitle"><i class=
"citetitle">Overload 39</i></span></p>
</div>
<div class="bibliomixed">
<p class="bibliomixed">&quot;Include files&quot;, Allan Kelly, <span class=
"citetitle"><i class="citetitle">Overload 39</i></span></p>
</div>
<div class="bibliomixed">
<p class="bibliomixed">&quot;<span class="citetitle"><i class=
"citetitle">Exceptional C++</i></span>&quot;, 2000, Herb Sutter, Addison
Wesley</p>
</div>
<div class="bibliomixed">
<p class="bibliomixed">&quot;<span class="citetitle"><i class=
"citetitle">The C Programming Language</i></span>&quot;, 1988, Kernighan
&amp; Richie , Prentice Hall</p>
</div>
<div class="bibliomixed">
<p class="bibliomixed">&quot;<span class="citetitle"><i class=
"citetitle">Safer C</i></span>&quot;, 1995, Les Hatton, McGraw-Hill</p>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e586" id="d0e586"></a>Post
Script</h2>
</div>
<p class="c2"><span class="remark">After this article had been
written and prepared for publication, we received the following
email from Herb Sutter (yes, for those that had not realised,
Overload is read by some of the best in the World). Clearly, we
wanted to publish and give Allan a chance to respond. So here is a
bit more on the subject from Herb and Allan.</span></p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e592" id="d0e592"></a>Herb Sutter
writes:</h3>
</div>
<p>Hi Allan,</p>
<p>I enjoyed your &quot;Include Files&quot; article in Overload issue 39
(September 2000), and would like to share some comments.</p>
<p>Re Rule 2: You argue that forward-declaring the standard
&quot;<tt class="classname">string</tt>&quot; <tt class=
"literal">typedef</tt> yourself is impractical, but it's much
stronger than that: It's illegal. The only thing you can do
yourself in <tt class="literal">namespace</tt> <tt class=
"literal">std</tt> is declare specializations of standard
templates; you can't declare standard names like <tt class=
"classname">basic_string</tt>.</p>
<p>Re Rule 3: I agree with the rule, but I don't think your
rationale is quite right. You write, &quot;I can use forward
declarations with pointers and references, but I[sic] not with
[objects],&quot; which is correct if you're talking about function
definitions but isn't correct if you're just talking about function
declarations (which is what you seem to be talking about in the
context of the other Rules). For example, most people are surprised
that the following code is a complete header and does not require a
definition for class X, because you only need the definition when
you need to generate code that must know the size of X (e.g.,
allocate stack space for a local object or a function value
parameter) or call a member function of X (e.g., copy it):</p>
<pre class="programlisting">
// ...usual include guards here and at
// the end...
  void g( X&amp;, double );
  void h( X*, double );
  class Y {
  public:
    X&amp; f( X&amp; ); 
// doesn't need definition of X
    X* f( X* ); // ditto
    X  f( X  ); // ditto
  };
  inline X&amp; Y::f( X&amp; r ) {
    g( r, 1.0 ); 
// use the reference: still don't need 
// definition of X
  }
  inline X* Y::f( X* p ) {
    h( p, 1.0 ); // use the pointer:
// still don't need definition of X
  }
// can't define Y::f(X) without X's 
// definition, though, because that
// would actually ask the compiler to
// generate code to call the copy 
// constructor and know the object's size
</pre>
<p>See also Item 26 in <i class="citetitle">Exceptional C++</i></p>
<p>Re Rule 9: I don't understand this rule. When you say, &quot;Think
about include guards, especially for libraries,&quot; I can't see how
this can apply to non-header files, yet Rule 8 just said that every
header should have include guards (and that's right). What is Rule
9 intended to apply to?</p>
<p>Incidentally, I strongly disagree with Lakos' external include
guards on two grounds:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>There's no benefit on most compilers. I admit that I haven't
done measurements, as Lakos seems to have done back then, but as
far as I know today's compilers already have smarts to avoid the
build time reread overhead - even MSVC does this optimization
(although it requires you to say &quot;<tt class="literal">#pragma
once</tt>&quot;), and it's the weakest compiler in many ways.</p>
</li>
<li>
<p>External include guards violate encapsulation because they
require many/all callers to know about the internals of the header
- in particular, the special <tt class="literal">#define</tt> name
used as a guard. They're also fragile - what if you get the name
wrong? what if the name changes?</p>
</li>
</ol>
</div>
<p>Re Rule 10: No, never! Even if you disagree with me above and go
ahead with external guard, your objections really do apply. If you
forget (or mistype) the external guards in even one place...</p>
<p>Re Rules 12-14: Agreed. It's also worth noting that header files
should always be written such that they do not depend on <tt class=
"literal">#include</tt> file ordering (e.g., being included before
vs. after any other header files). This actually impacts namespaces
and it's a reason I argue that using declarations should never
appear anywhere in a header file, not even in a namespace (see my
article <i class="citetitle">Migrating To Namespaces</i> in Dr
Dobbs Journal, 25(10), October 2000).</p>
<p><span class="emphasis"><em>Herb</em></span></p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e654" id="d0e654"></a>Allan's
response:</h3>
</div>
<p>First, thanks for taking the time to read my thoughts and pass
on your comments. Interestingly, John Merrells has part two of the
piece in the works for Overload 40, in this I added a paragraph as
a result of reading <i class="citetitle">Exceptional C++</i> to
clarify rules 2-3. I agree completely with your comments on my rule
3 - forward declarations can be used as long as no code is
generated; my writing was a little confused.</p>
<p>In rule 2 (concerning forward declarations in separate files) I
was trying to use the <tt class="classname">string</tt> type as an
example of how forward declarations could get complex.
Unfortunately I chose a bad example. In fact I didn't know
re-opening <tt class="literal">std</tt> <tt class=
"literal">namespace</tt> was illegal, I should have done but I
didn't. I wonder how many developers actually know this? Now you've
brought it to my attention it's obvious. Perhaps more importantly:
do any compilers enforce this rule? I suspect few if any. I suggest
we shout it loud: &quot;don't add anything to <tt class=
"literal">std</tt> <tt class="literal">namespace</tt>.&quot;</p>
<p>(This is kind of unfortunate, to my mind, one of the strongest
advantages of a namespace over a class is that it can be re-opened
and items added but many novices first encounter with namespaces
will be with std, one or both points may well be lost on them.)</p>
<p>Rule 9: Reading it again I see I should have phrased it as
&quot;Think about external include guards, especially within libraries.&quot;
My objective was to explore the Lakos argument about external
include guards. Like yourself I have problems with Lakos' argument,
while he presents a convincing case for improving compile times
increasingly I agree with you. In the long run I think external
guards make the source code more difficult to read and increase the
coupling between a header file and the file's clients. Yet the
benefits, if any, will on be apparent on very large projects.</p>
<p>I deliberately said &quot;think about&quot; because I didn't want to give
a definite rule, I wanted to make people aware of the argument, to
this end your comments are an excellent contribution to the case.
Rule 10: I never intended to advocate the removal of internal
include guards. We agree here: always leave them in even if you
have external guards!</p>
<p>I find it very interesting that you agree with my rules 12 to 14
&quot;order includes with most inflexible first, most flexible last.&quot;
These rules where the genesis of the article, they have become a
fixture of my coding style. However, they go directly against
Lakos' advice which states that you should order from local to
system. (Not only this, Kevlin's article in the same issue of
Overload upholds the Lakos rule.) I hope we have started a debate
here.</p>
<p>I hope you'll forgive this novice-writer a few slips and enjoy
my continuation of the subject in Overload 40. Once again, many
thanks for your comments.</p>
<p><span class="emphasis"><em>Allan</em></span></p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
