The obvious answer is: don't write #include <Helper.h>
. Your
files aren't part of the implementation, and should be included
using #include "Helper.h"
. If you do this, the first place
the compiler will look for the include file is in the directory
containing the file which is including it: if you include it
from Foo/Foo.h
, the compiler will pick up Foo/Helper.h
; if you include
it from Bar/Bar.h
, the compiler will pick up Bar/Helper'
and
so on.
Client code should only set the include path to the root, and do
things like #include "Foo/Foo.h"
; if necessary, they can also
do #include "Foo/Helper.h"
.
The one thing you do have to do with this strategy is to ensure
the uniqueness of all of the header guards. If this is an
application, it will usually be sufficient to mangle the path
into the include guard, e.g. use Foo_Helper_h
, instead of just
Foo_h
. Alternatively (and I would use this for any library
which third parties should use), generate some random string for
the include guard. (If I open a file named abc.h
which
doesn't exist, my editor automatically generates the following
boilerplate:
/********************************************************/
/* File: abc.h */
/* Author: J. Kanze */
/* Date: 02/05/2013 */
/* ---------------------------------------------------- */
#ifndef abc_h_20130502O481MBxFZeAzz4dgIb7iC4Q9
#define abc_h_20130502O481MBxFZeAzz4dgIb7iC4Q9
#endif
It's a safe bet that the include guard here will never conflict
with any other. (And you have to do something along these lines
anyway, in order to get your copyright message in the file.)
Foo
,Bar
orHelper
to the include path, and includeFoo/Helper.h
andBar/Helper.h
. Or come up with names better thanHelper.h
. – juanchopanzaHelper.h
is just a placeholder name, you can think about it asFat16.h
or something like that. Can you elaborate it a bit more and make it into an answer? Bit hard to understand here. – Xun Yang