R
Ravi
How can I prevent a header file from being included twice?
Ravi said:How can I prevent a header file from being included twice?
Ravi said:How can I prevent a header file from being included twice?
Ravi said:How can I prevent a header file from being included twice?
Chris said:....Ravi said:How can I prevent a header file from being included twice?
The #ifdef can go in one of two places, each with its own
tradeoffs.
(a) It can go in the header file, so you get something like:
in spoo.h:
#ifndef H_SPOO
#define H_SPOO
... contents of spoo ...
#endif
(b) It can go around /each guarded #include/ of the header files:
in wantspoo.[ch]:
#ifndef H_SPOO
#include "spoo.h"
#endif
The (a) tactic makes each header self-contained: the conditional
machinery is buried inside it. You only have to get it right
once per file, not once per #include. Sequences of #includes
are nice and compact.
The (b) tactic avoids actually #including "lots" of text that
will then be ignored. If your header files are big big big, or
your file system slow slow slow, or the room available to your
compiler is small small small [1], that might reduce your
compilation overheads significantly.
Your call. [I choose (a) myself; I've not been in the situation
that would make (b) worth exploring. The mixed tactic of doing
some (a) and some (b) is an obvious invitation to trouble.]
Thad said:Chris said:...Ravi said:How can I prevent a header file from being included twice?
The #ifdef can go in one of two places, each with its own
tradeoffs.
(a) It can go in the header file, so you get something like:
in spoo.h:
#ifndef H_SPOO
#define H_SPOO
... contents of spoo ...
#endif
(b) It can go around /each guarded #include/ of the header files:
in wantspoo.[ch]:
#ifndef H_SPOO
#include "spoo.h"
#endif
The (a) tactic makes each header self-contained: the conditional
machinery is buried inside it. You only have to get it right
once per file, not once per #include. Sequences of #includes
are nice and compact.
The (b) tactic avoids actually #including "lots" of text that
will then be ignored. If your header files are big big big, or
your file system slow slow slow, or the room available to your
compiler is small small small [1], that might reduce your
compilation overheads significantly.
Your call. [I choose (a) myself; I've not been in the situation
that would make (b) worth exploring. The mixed tactic of doing
some (a) and some (b) is an obvious invitation to trouble.]
I always use (a). If I want the speedup, I add the conditional in the
including code, but also leave it in the header. There is no reason to
have (b) only. No trouble is invited.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.