Inclusion guards

D

Dave

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
....
#endif


I need to automate the creation of the guard macro name, perhaps by doing
something like this:

#define MACRO_NAME_CREATOR(TEXT) TEXT##_INCLUDED

#ifndef MACRO_NAME_CREATOR(__FILE__)
#define MACRO_NAME_CREATOR(__FILE__)
....
#endif


However, this does not work. Can anybody offer a technique that will
accomplish what is intended above?

Thanks,
Dave
 
E

E. Robert Tisdale

Dave said:
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 1
// ...
#endif//FOO_INCLUDED


I need to automate the creation of the guard macro name, perhaps by doing
something like this:

#define MACRO_NAME_CREATOR(TEXT) TEXT##_INCLUDED

#ifndef MACRO_NAME_CREATOR(__FILE__)
#define MACRO_NAME_CREATOR(__FILE__)
...
#endif

However, this does not work. Can anybody offer a technique that will
accomplish what is intended above?

No.

Notice that your header file isn't idempotent
because it includes the MACRO_NAME_CREATOR definition
before it checks to see if it is defined.
 
J

JH Trauntvein

Dave said:
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
...
#endif


I need to automate the creation of the guard macro name, perhaps by doing
something like this:

#define MACRO_NAME_CREATOR(TEXT) TEXT##_INCLUDED

#ifndef MACRO_NAME_CREATOR(__FILE__)
#define MACRO_NAME_CREATOR(__FILE__)
...
#endif


However, this does not work. Can anybody offer a technique that will
accomplish what is intended above?

Thanks,
Dave

You might want to look into a compiler specific feature like "pragma
once" used with visual c++. GCC also has a similar feature but the
memory of how to invoke it escapes me.
 
J

JH Trauntvein

Dave said:
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
...
#endif


I need to automate the creation of the guard macro name, perhaps by doing
something like this:

#define MACRO_NAME_CREATOR(TEXT) TEXT##_INCLUDED

#ifndef MACRO_NAME_CREATOR(__FILE__)
#define MACRO_NAME_CREATOR(__FILE__)
...
#endif


However, this does not work. Can anybody offer a technique that will
accomplish what is intended above?

Thanks,
Dave

You might want to look into a compiler specific feature like "pragma
once" used with visual c++. GCC also has a similar feature but the
memory of how to invoke it escapes me.
 
S

Sebastian Redl

JH said:
Dave wrote:
You might want to look into a compiler specific feature like "pragma
once" used with visual c++. GCC also has a similar feature but the
memory of how to invoke it escapes me.

The GCC feature recognizes an inclusion guard and marks the file. Having
"fancy" inclusion guards thus makes compiling under GCC less efficient.

I don't see the necessity for generating inclusion guards by macros. The
name Trauntvein used for his hypothetical macro is already longer than most
inclusion guards I use. I have this simple scheme:

_<file name using _ instead of .>_SR_H_

The SR_H marks the file as a header created by me. It takes me very little
time to write this guard, especially as
#ifndef
and
#define
are the same number of characters, so copy&paste of the macro name to the
line below is very fast.
 
V

Victor Bazarov

Sebastian said:
JH Trauntvein wrote:




The GCC feature recognizes an inclusion guard and marks the file. Having
"fancy" inclusion guards thus makes compiling under GCC less efficient.

I don't see the necessity for generating inclusion guards by macros. The
name Trauntvein used for his hypothetical macro is already longer than most
inclusion guards I use. I have this simple scheme:

_<file name using _ instead of .>_SR_H_

If your file name begins with a capital letter, the name formed here would
be reserved and you'd be in violation of the Standard rules about naming.
What's that desire to begin everything with an underscore, anyway?

How often do you really have to read those once they're written? Their
position in the file is not going to change. No other files are supposed
to define them, or use them, for that matter.

Making them unique throughout the project is not such a bad idea. Headers
generated by Microsoft Visual Studio contain some long macro names made up
by the same mechanism as their GUID (see OLE) generator, which definitely
prevents them from being the same in two separate files. You may consider
it overkill but think about it. If you worry about the inclusion guards'
being specific to the file, what if you have two files with the same name
in the same project. They may never be included into the same file at the
beginning of the project, but projects grow, and headers include headers,
so you may end up with _data_h_SR_H_ defined in one "data.h" and then also
_data_h_SR_H_ in the other "data.h" elsewhere...

Anyway, I didn't mean to barge in like that, just dropping my $0.02 in...
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,117
Latest member
Matilda564
Top