473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

File included twice

How can I prevent a header file from being included twice?

Aug 22 '07 #1
5 3528
Ravi <ra*********@gmail.comwrites:
How can I prevent a header file from being included twice?
Use a header guard.

http://en.wikipedia.org/wiki/Include_guard has some info on
these. At first glance, I don't see any bad info there.
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
Aug 22 '07 #2
Ravi wrote:
How can I prevent a header file from being included twice?
With a combination of #ifdef and #define.

The #define goes in the included file; the #defined symbol means
"the associated file has already been included".

[Do NOT NOT NOT start that symbol with a _, even though if you
look at your system header files you're likely to see them
doing that. /They/ are using leading _ because /they/ are
the implementation, keeping out of your way. Keep out of
theirs.]

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.]

[1] Which might be true, since not all C compilers run on
giga-{byte,hertz} processors with oodles of fast disc.

--
Guarded H_edgehog
"Never ask that question!" Ambassador Kosh, /Babylon 5/

Aug 22 '07 #3
Ravi wrote:
>
How can I prevent a header file from being included twice?
Well, one method is not to #include it twice. Another is to
install include guards, which could be something like (for foo.h
file):

#ifndef H_foo_h
# define H_foo_h
/* ... Normal content of foo.h */
#endif

In fact most of us routinely include such guards in each header.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Aug 23 '07 #4
Chris Dollin wrote:
Ravi wrote:
>>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.

--
Thad
Aug 23 '07 #5
Thad Smith wrote:
Chris Dollin wrote:
>Ravi wrote:
>>>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.
By "some (a) and some (b)" I meant [but failed] to imply that there
were some non-(a) and some non-(b), not (as in your case) all-(a)
with backstopping (b), a case that hadn't occurred to me.

Yours is safe. I presume you can now see why the case I had in mind
is inviting trouble.

--
Heads Or Tails? Hedgehog
A rock is not a fact. A rock is a rock.

Aug 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file...
18
by: Al | last post by:
I'm still trying to do this but it never worked! In a .cpp file, I write the code, and at the beginning, I write: #ifndef MYLIST_H #define MYLIST_H ....to end: #endif What's wrong with it for...
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
5
by: B. Williams | last post by:
I need some assistance with random access file processing. I have a function that I would like to change from sequential file processing to random access. Thanks in advance. void...
7
by: The Cool Giraffe | last post by:
Please note that i do intend to use a header file. However, i'm not sure if it's really needed or just a convention. Suppose we have the following two files. // Something.h class Something {...
10
by: vunet.us | last post by:
What is the workaround of passign a parameter to any included asp file: <!-- #Include File="file.asp" --> This obviously does not work: <!-- #Include File="file.asp?id=123" --> Thank you
3
by: Pietro Cerutti | last post by:
Hi Group, suppose test1.c, test2.c and test.h /*** BEGIN TEST.H ***/ #ifndef _TEST_H #define _TEST_H typedef struct {
6
by: JT | last post by:
Hi, Here's my problem. I am using an obscure font in my Windows Form application, so I want to ensure that people using the application will have the text displayed as intended. I've included...
45
by: David Mearsen | last post by:
Hi, I've recently started programming C after many years using "the other language"... I just wanted to find out the common practice for organising source files. Specifically, consider a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.