473,396 Members | 2,154 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,396 software developers and data experts.

inclusion guards

I was looking at some library code today and noticed something like
this:

// sublibrary.h

// define some constants, enums, symbols

#include "componentA.h"
#include "componentB.h"
#include "componentC.h"
#include "componentD.h"

Then each componentX.h file #includes sublibrary.h in order to get the
defined constants, enums and such. At first I thought this would be a
problem because it seemed circular, however, it works because each
component header file #includes "sublibrary.h" outside the inclusion
guards:

// example: componentA.h

#include "sublibrary.h"

#ifndef COMPONENTA_H
#define COMPONENTA_H

// class of ComponentA

#endif

If #include "sublibrary.h" was inside the inclusion guards, it would
not work since ComponentA would get defined twice.

So my question is: Is this style common or generally recommended?

May 4 '07 #1
6 2589

"vsgdp" <cl********@yahoo.comwrote in message
news:11*********************@c35g2000hsg.googlegro ups.com...
>I was looking at some library code today and noticed something like
this:

// sublibrary.h

// define some constants, enums, symbols

#include "componentA.h"
#include "componentB.h"
#include "componentC.h"
#include "componentD.h"

Then each componentX.h file #includes sublibrary.h in order to get the
defined constants, enums and such. At first I thought this would be a
problem because it seemed circular, however, it works because each
component header file #includes "sublibrary.h" outside the inclusion
guards:

// example: componentA.h

#include "sublibrary.h"

#ifndef COMPONENTA_H
#define COMPONENTA_H

// class of ComponentA

#endif

If #include "sublibrary.h" was inside the inclusion guards, it would
not work since ComponentA would get defined twice.
I think that's backwards. Having the #include "sublibrary.h" outside the
include guards causes it to *always* include sublibrary.h, even if the
current file is itself being included from sublibrary.h itself! And that's
a problem. perhaps there are include guards in sublibrary.h as well, which
are preventing its contents from being included in a circular fashion?
So my question is: Is this style common or generally recommended?
I can't recall ever having seen such a setup. It makes no sense to me. A
more common scenario would be if sublibrary.h forward-declared whatever it
needed to know about from those other header files, instead of including
them. (Or vise-versa.)

But as far as I know, circular inclusion of headers isn't normal, regardless
of the placement of header include guards.

-Howard
May 4 '07 #2
vsgdp wrote:
....
... each
component header file #includes "sublibrary.h" outside the inclusion
guards:

// example: componentA.h

#include "sublibrary.h"

#ifndef COMPONENTA_H
#define COMPONENTA_H

// class of ComponentA

#endif
....
>
So my question is: Is this style common or generally recommended?
Both common and recommended.
May 4 '07 #3
Howard wrote:
"vsgdp" <cl********@yahoo.comwrote in message
news:11*********************@c35g2000hsg.googlegro ups.com...
>I was looking at some library code today and noticed something like
this:

// sublibrary.h

// define some constants, enums, symbols

#include "componentA.h"
#include "componentB.h"
#include "componentC.h"
#include "componentD.h"

Then each componentX.h file #includes sublibrary.h in order to get the
defined constants, enums and such. At first I thought this would be a
problem because it seemed circular, however, it works because each
component header file #includes "sublibrary.h" outside the inclusion
guards:

// example: componentA.h

#include "sublibrary.h"

#ifndef COMPONENTA_H
#define COMPONENTA_H

// class of ComponentA

#endif

If #include "sublibrary.h" was inside the inclusion guards, it would
not work since ComponentA would get defined twice.

I think that's backwards. Having the #include "sublibrary.h" outside the
include guards causes it to *always* include sublibrary.h,
Assuming that sublibrary.h uses it's own inclusion guard, why would this
be a problem ?

even if the
current file is itself being included from sublibrary.h itself! And that's
a problem. perhaps there are include guards in sublibrary.h as well, which
are preventing its contents from being included in a circular fashion?
>So my question is: Is this style common or generally recommended?

I can't recall ever having seen such a setup.
It should not matter but having just checked, I put my includes inside
my guards. So what I wrote in the other post about it being common is
wrong, however it should make no difference whatsoever (other than the
compiler taking a little more time in processing the include more often
that necessary).
May 4 '07 #4

"Gianni Mariani" <gi*******@mariani.wswrote in message
news:46***********************@per-qv1-newsreader-01.iinet.net.au...
Howard wrote:
>"vsgdp" <cl********@yahoo.comwrote in message
news:11*********************@c35g2000hsg.googlegr oups.com...
>>I was looking at some library code today and noticed something like
this:

// sublibrary.h

// define some constants, enums, symbols

#include "componentA.h"
#include "componentB.h"
#include "componentC.h"
#include "componentD.h"

Then each componentX.h file #includes sublibrary.h in order to get the
defined constants, enums and such. At first I thought this would be a
problem because it seemed circular, however, it works because each
component header file #includes "sublibrary.h" outside the inclusion
guards:

// example: componentA.h

#include "sublibrary.h"

#ifndef COMPONENTA_H
#define COMPONENTA_H

// class of ComponentA

#endif

If #include "sublibrary.h" was inside the inclusion guards, it would
not work since ComponentA would get defined twice.

I think that's backwards. Having the #include "sublibrary.h" outside the
include guards causes it to *always* include sublibrary.h,

Assuming that sublibrary.h uses it's own inclusion guard, why would this
be a problem ?
That assumption is exactly what I speculated on in the text below:
even if the
>current file is itself being included from sublibrary.h itself! And
that's a problem. perhaps there are include guards in sublibrary.h as
well, which are preventing its contents from being included in a circular
fashion?
I was trying to say that circular inclusions are not (in general) correct in
the first place, and that putting the #includes before the include guards
was not what prevented the problem. Rather, what made it work was likely
that there was an include guard in sublibrary.h as well.

-Howard
May 5 '07 #5
Assuming that sublibrary.h uses it's own inclusion guard, why would this
be a problem ?

That assumption is exactly what I speculated on in the text below:
Yes, sorry for not making that clear, sublibrary.h did use its own
inclusion guards. Anyway, I don't like the style so I will personally
avoid it, but I was just wondering if this is common to see.

May 5 '07 #6
"vsgdp" <cl********@yahoo.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
>
Assuming that sublibrary.h uses it's own inclusion guard, why would
this
be a problem ?

That assumption is exactly what I speculated on in the text below:

Yes, sorry for not making that clear, sublibrary.h did use its own
inclusion guards. Anyway, I don't like the style so I will personally
avoid it, but I was just wondering if this is common to see.
It is common for a header file that needs other header files to be compiled
include them itself. For example, if I have a header file for a class that
uses std::string, then I would include <stringin my header file. The
include guards inside of <stringprevent problems with this, and it makes
it a lot easier to include header files inside a source file without having
to worry about what order they're included in.

If you have proper include guards inside of your sublibrary.h then
componentA.h won't really bring it in with #include "sublibrary.h" (this is
what's bad about snipping too much from previous posts, people will have to
go back to the original message to find out what I'm talking about).

The real question becomes, does it make sense for componentA.h to need to
include sublibrary.h if sublibrary.h also needs to include componentA.h. If
it was required then it wouldn't compile because of circular dependancies,
but since it is compiling, it seems you're including one or the other
needlessly. Maybe you just want the user of componentA.h to also have all
the defines of componentA, B, C, D and sublibrary.h when they include it,
because that's whats going to happen. Any time the user includes any it
will include all, probably needlessly.

Most likely, you just need sublibrary.h to include componentA.h B, C and D.
Then the user when they include componentA.h to only get what they need. If
they need something in B, C, D or sublibrary.h then have them include it
also.
May 5 '07 #7

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

Similar topics

2
by: Kamilche | last post by:
Are inclusion guards necessary to prevent multiple modules from importing the same shared module multiple times? --Kamilche
14
by: Fritz Foetzl | last post by:
I'm flummoxed. I'm a veteran C++ programmer from the Unix/Linux camp, trying to learn Visual C++. I'm trying to build a project in which I need to include one header in a couple of different files,...
6
by: Johannes Bauer | last post by:
Hi group, I've got a question concerning inclusion of .hpp files. Currently I'm including all needed header files in the .cpp file. This means all dependencies of the package and all...
5
by: Dave | last post by:
Hello all, To protect against multiple inclusions, it is standard practice to enclose the contents of a header file in a construct like this: #ifndef FOO_INCLUDED #define FOO_INCLUDED .......
2
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #include...
6
by: techBoy | last post by:
I am looking for a tool that can scan my soyrce code and check if a header file gets included more then once in a sequece of compiled code. Can some one guide me to such a tool !!
3
by: zmp | last post by:
Ive seen a thread about it here somewhere and tried doing whats being said in that thread, but still having a problem. Im using a OpenGL Font Class which i want included in different places. One...
6
by: Juha Nieminen | last post by:
Multiple inclusion of the same header file can cause the compilation to fail because of multiple definitions of the same type. That's why it's standard practice to write all headers like this: ...
9
by: ramsatishv | last post by:
Hi, If I include a ".h" file for multiple times, will it increase my program size?? Regards Ram.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.