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

header guard

I am confused by the header guard, for example,

#ifndef SALESITEM_H
#define SALESITEM_H
.....
#endif

what is the "SALEITEM_H" ? It is a variable name, or something else? I
could not find "SALESITEM_H" in all the files, so, I don't know where
"SALEITEM_H" come from.

Thanks.

Nov 21 '06 #1
15 11015
"asdf" <li*********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>I am confused by the header guard, for example,

#ifndef SALESITEM_H
#define SALESITEM_H
....
#endif

what is the "SALEITEM_H" ? It is a variable name, or something else? I
could not find "SALESITEM_H" in all the files, so, I don't know where
"SALEITEM_H" come from.

Thanks.
It is anything you want it to be and should be unqiue, but generally it's
the name of the header file. For SALESITEMS_H I would presume the header
file name was
SalesItems.h

It's pretty much an unwritten rule that variables in all caps ending in _H
are used for header guards.

So if you made a header file called

AsdfTools.h
you would make the header guard

#ifndef ASDFTOOLS_H
#define ASDFTOOLS_H
//...
#endif
Nov 21 '06 #2
asdf wrote:
I am confused by the header guard, for example,

#ifndef SALESITEM_H
#define SALESITEM_H
....
#endif

what is the "SALEITEM_H" ? It is a variable name, or something else?
It's the name of a macro.
I could not find "SALESITEM_H" in all the files, so, I don't know where
"SALEITEM_H" come from.
It comes from just the lines that you quoted.

Nov 21 '06 #3
Thanks.
Could you please explain more about the aim (or the advantage) of the
header guard in your example ?

Jim Langston wrote:
So if you made a header file called

AsdfTools.h
you would make the header guard

#ifndef ASDFTOOLS_H
#define ASDFTOOLS_H
//...
#endif
Nov 21 '06 #4
asdf wrote:
I am confused by the header guard, for example,

#ifndef SALESITEM_H
#define SALESITEM_H
....
#endif

what is the "SALEITEM_H" ? It is a variable name, or something else? I
could not find "SALESITEM_H" in all the files, so, I don't know where
"SALEITEM_H" come from.
It is a preprocessor symbol. It should only exist on the lines you show
above. If it isn't defined on the first line, the second line defines
it into existence. If it's already defined when you reach the first
line (e.g., if you include the same header twice in one .cpp file),
then the preprocessor will strip all the stuff between the #if and the
#endif, including the #define on the second line.

Cheers! --M

Nov 21 '06 #5

asdf wrote:
I am confused by the header guard, for example,

#ifndef SALESITEM_H
#define SALESITEM_H
....
#endif

what is the "SALEITEM_H" ? It is a variable name, or something else? I
could not find "SALESITEM_H" in all the files, so, I don't know where
"SALEITEM_H" come from.

Thanks.
#if SOMETHING
This part of the file will be processed by the compiler
only if the SOMETHING symbol is defined
#endif

compiler might find more than one #include
statement for a single header in multiple
compile units. to prevent multiple declarations
you need to prevent it processing it more than once.

#ifndef SALESITEM_H // if this symbol is not defined
#'define SALESITEM_H // define it

declarations

#endif

so the first time it encounters this header
the symbol will not be defined and the file
will be processed. On following visits
the symbol will be defined and the content
of the file will not be processed again.

Google for 'preprocessor'

Nov 21 '06 #6
asdf wrote:
Thanks.
Could you please explain more about the aim (or the advantage) of the
header guard in your example ?
If a translation unit includes the header multiple times, let's say there is
a header x.h which includes AsdfTools.h and a header y.h that does, too,
and then you include both in your .c file, you end up having AsdfTools.h
included twice in it. This usually leads to problems (multiple
definitions), so the include guard idiom is used to ensure that the part
between the #ifndef and the #endif is excluded if the header is read a
second or third time.
Jim Langston wrote:
>So if you made a header file called

AsdfTools.h
you would make the header guard

#ifndef ASDFTOOLS_H
#define ASDFTOOLS_H
//...
#endif
Nov 21 '06 #7
Thank you very much.

Rolf Magnus wrote:
asdf wrote:
Thanks.
Could you please explain more about the aim (or the advantage) of the
header guard in your example ?

If a translation unit includes the header multiple times, let's say there is
a header x.h which includes AsdfTools.h and a header y.h that does, too,
and then you include both in your .c file, you end up having AsdfTools.h
included twice in it. This usually leads to problems (multiple
definitions), so the include guard idiom is used to ensure that the part
between the #ifndef and the #endif is excluded if the header is read a
second or third time.
Jim Langston wrote:
So if you made a header file called

AsdfTools.h
you would make the header guard

#ifndef ASDFTOOLS_H
#define ASDFTOOLS_H
//...
#endif
Nov 21 '06 #8
Jim Langston wrote:
>
It's pretty much an unwritten rule that variables in all caps ending in _H
are used for header guards.

So if you made a header file called

AsdfTools.h
you would make the header guard

#ifndef ASDFTOOLS_H
#define ASDFTOOLS_H
It shouldn't be. What if your filename starts with E ? All identifiers
beginning with an uppercase E followed by a digit or an uppercase
letter, are reserved for implementation use.

Nov 21 '06 #9
"Old Wolf" <ol*****@inspire.net.nzwrites:
Jim Langston wrote:

It's pretty much an unwritten rule that variables in all caps ending in _H
are used for header guards.

So if you made a header file called

AsdfTools.h
you would make the header guard

#ifndef ASDFTOOLS_H
#define ASDFTOOLS_H

It shouldn't be. What if your filename starts with E ? All identifiers
beginning with an uppercase E followed by a digit or an uppercase
letter, are reserved for implementation use.
Uhh... really? The standard says anything containing a double
underscore, or that starts with an underscore and is in the global
scope, is reserved (17.4.3.1.2). Chapter and verse for the
uppercase E thing?

----------------------------------------------------------------------
Dave Steffen, Ph.D. Disobey this command!
Software Engineer IV - Douglas Hofstadter
Numerica Corporation
dg@steffen a@t numerica d@ot us (remove @'s to email me)
Nov 21 '06 #10
Dave Steffen wrote:
"Old Wolf" <ol*****@inspire.net.nzwrites:
Jim Langston wrote:
>
It's pretty much an unwritten rule that variables in all caps ending in _H
are used for header guards.
>
So if you made a header file called
>
AsdfTools.h
you would make the header guard
>
#ifndef ASDFTOOLS_H
#define ASDFTOOLS_H
It shouldn't be. What if your filename starts with E ? All identifiers
beginning with an uppercase E followed by a digit or an uppercase
letter, are reserved for implementation use.

Uhh... really?
Section 19.3 (errno.h) referes to the C standard. I only have the C99
standard, but I believe the section is similar or the same as it was in
C90. The C99 section is 7.5#4:

Additional macro definitions, beginning with E and a digit or E and
an uppercase letter,173) may also be specified by the implementation.

And 7.1.3 says:

Each macro name in any of the following subclauses (including the
future library directions) is reserved for use as specified if any of
its associated headers is included; unless explicitly stated
otherwise

So, I should have said that Exxx is reserved only if errno.h or cerrno
is included. Seeing as it might be, I think that Exxx is a poor choice
of name for an include guard. (I write mine as H_FILENAME).

Nov 22 '06 #11
Thanks.It is anything you want it to be and should be unqiue, but generally it's
the name of the header file. For SALESITEMS_H I would presume the header
file name was SalesItems.h
For very large projects, you sometimes have files with the same name
but in different directories. This means you need a slightly different
convention. We have settled on a system where we put the namespace(s)
followed by the class name in the header file. For example, for
Foo::Bar::MyClass the symbol would be FOO_BAR_MYCLASS_H. We tried using
the directory path from some top level root, but the symbols were
getting too long for some files. The namespaces typically have shorter
names than our directories and are not nested as deeply. For the few
header files containing something other than a class, we usually use
the file name instead of the class name but still retain the namespace
parts. This seems to work well for us and is fairly predictable.

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia

Nov 22 '06 #12
"Old Wolf" <ol*****@inspire.net.nzwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Dave Steffen wrote:
>"Old Wolf" <ol*****@inspire.net.nzwrites:
Jim Langston wrote:

It's pretty much an unwritten rule that variables in all caps ending
in _H
are used for header guards.

So if you made a header file called

AsdfTools.h
you would make the header guard

#ifndef ASDFTOOLS_H
#define ASDFTOOLS_H

It shouldn't be. What if your filename starts with E ? All identifiers
beginning with an uppercase E followed by a digit or an uppercase
letter, are reserved for implementation use.

Uhh... really?

Section 19.3 (errno.h) referes to the C standard. I only have the C99
standard, but I believe the section is similar or the same as it was in
C90. The C99 section is 7.5#4:

Additional macro definitions, beginning with E and a digit or E and
an uppercase letter,173) may also be specified by the implementation.

And 7.1.3 says:

Each macro name in any of the following subclauses (including the
future library directions) is reserved for use as specified if any of
its associated headers is included; unless explicitly stated
otherwise

So, I should have said that Exxx is reserved only if errno.h or cerrno
is included. Seeing as it might be, I think that Exxx is a poor choice
of name for an include guard. (I write mine as H_FILENAME).
Even if that is the case, errno.h is not going to be defining macros ending
in _H (except for it's own header guard maybe) so it should be perfectly
fine. I just opened up my systems errno.h to see what kind of defines they
were doing and they're like:

#define EPERM 1
#define ENOENT 2
#define ESRCH 3
#define EINTR 4
#define EIO 5
etc... No conflicts with *_H

Stating that we can't define any all upcase names starting with E is just
ludicrious, I don't care what the freaking standard says.
Nov 22 '06 #13
Jim Langston wrote:
I don't care what the freaking standard says.
Then you're in the wrong place here.
Nov 22 '06 #14
"Old Wolf" <ol*****@inspire.net.nzwrites:
Dave Steffen wrote:
"Old Wolf" <ol*****@inspire.net.nzwrites:
Jim Langston wrote:

It's pretty much an unwritten rule that variables in all caps ending in _H
are used for header guards.

So if you made a header file called

AsdfTools.h
you would make the header guard

#ifndef ASDFTOOLS_H
#define ASDFTOOLS_H
>
It shouldn't be. What if your filename starts with E ? All identifiers
beginning with an uppercase E followed by a digit or an uppercase
letter, are reserved for implementation use.
Uhh... really?

Section 19.3 (errno.h) referes to the C standard. I only have the C99
standard, but I believe the section is similar or the same as it was in
C90. The C99 section is 7.5#4:

Additional macro definitions, beginning with E and a digit or E and
an uppercase letter,173) may also be specified by the implementation.
Yoiks, Zounds, and other comments. OK; E followed by a digit or
capital letter is out.
>
And 7.1.3 says:

Each macro name in any of the following subclauses (including the
future library directions) is reserved for use as specified if any of
its associated headers is included; unless explicitly stated
otherwise

So, I should have said that Exxx is reserved only if errno.h or cerrno
is included. Seeing as it might be, I think that Exxx is a poor choice
of name for an include guard. (I write mine as H_FILENAME).
I'm not sure I follow this point. Is the notion that errno.h can
define something like "Errno" as part of its include guards?

----------------------------------------------------------------------
Dave Steffen, Ph.D. Disobey this command!
Software Engineer IV - Douglas Hofstadter
Numerica Corporation
dg@steffen a@t numerica d@ot us (remove @'s to email me)
Nov 27 '06 #15
Dave Steffen <dg*******@numerica.uswrote:
"Old Wolf" <ol*****@inspire.net.nzwrites:
>And 7.1.3 says:

Each macro name in any of the following subclauses (including the
future library directions) is reserved for use as specified if any of
its associated headers is included; unless explicitly stated
otherwise

So, I should have said that Exxx is reserved only if errno.h or cerrno
is included. Seeing as it might be, I think that Exxx is a poor choice
of name for an include guard. (I write mine as H_FILENAME).

I'm not sure I follow this point. Is the notion that errno.h can
define something like "Errno" as part of its include guards?
I think the notion is that errno.h can define something like "Errno" as
*any part* of its implementation.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Nov 27 '06 #16

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

Similar topics

2
by: tuko | last post by:
Hello. Do you know if there is any standard STL header guard for vectors, lists, maps e.t.c? I would like to write code like that #ifndef STL_VECTOR_ #error "please include stl vector"...
18
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and...
3
by: Code4u | last post by:
We have a mature project that suffers long build times because many modules pull in far more than they need. In the long term I would like to refactor to break some of the dependencies, but I would...
11
by: wenmang | last post by:
Hi, all: What will happen if header files with the exact same names but with different contents and declared/defined without include guard reside in the include path for the compiler? Are these...
4
by: Winston Nimchan | last post by:
Hi: I'm currently developing a socket application and would like to precede the data being sent with a 4 byte message length header (bin4). Can anyone help Regards Winston
8
by: nrhayyal | last post by:
Hi c++ Gurus, Need your blessing. while testing few aspects with respect to header file inclusions, i observed few things which i would like to share with you. i have a file sqlca.h in which a...
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...
3
by: weird0 | last post by:
Hi!all. As part of the course project in the university, students have been given task to develop a front-end for SMILE( an API written in C++ for network modelling). Upon extracting the setup, it...
7
by: bcpkh | last post by:
Hello All Received a header file from a supplier that defines an interface to implement but it's giving me a problem, I reproduce the general structure of the header file below; #ifndef XYZ_H...
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: 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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
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.