473,732 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inclusion guards


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_CREA TOR(TEXT) TEXT##_INCLUDED

#ifndef MACRO_NAME_CREA TOR(__FILE__)
#define MACRO_NAME_CREA TOR(__FILE__)
....
#endif
However, this does not work. Can anybody offer a technique that will
accomplish what is intended above?

Thanks,
Dave
Jul 22 '05 #1
5 3726
Dave wrote:
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_CREA TOR(TEXT) TEXT##_INCLUDED

#ifndef MACRO_NAME_CREA TOR(__FILE__)
#define MACRO_NAME_CREA TOR(__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_CREA TOR definition
before it checks to see if it is defined.
Jul 22 '05 #2

Dave wrote:
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_CREA TOR(TEXT) TEXT##_INCLUDED

#ifndef MACRO_NAME_CREA TOR(__FILE__)
#define MACRO_NAME_CREA TOR(__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.

Jul 22 '05 #3
Dave wrote:
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_CREA TOR(TEXT) TEXT##_INCLUDED

#ifndef MACRO_NAME_CREA TOR(__FILE__)
#define MACRO_NAME_CREA TOR(__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.

Jul 22 '05 #4
JH Trauntvein wrote:
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.
--
Sebastian Redl
Jul 22 '05 #5
Sebastian Redl wrote:
JH Trauntvein wrote:

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_


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

Jul 22 '05 #6

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

Similar topics

2
1994
by: Kamilche | last post by:
Are inclusion guards necessary to prevent multiple modules from importing the same shared module multiple times? --Kamilche
14
1771
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, but the classic multiple inclusion problem is biting me hard. The #ifndef..#define..#endif method doesn't seem to be working, although all the documentation I've read indicates that it should. As a small example, I have an empty console...
6
1751
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 dependencies of these dependencies and so on. This is quite ugly. A start of an "Example.cpp" file could look like this
2
4836
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 "B.h"
6
5205
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 !!
6
2609
by: vsgdp | last post by:
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"
18
2155
by: gutmant | last post by:
Say you have a file, a.h with an include guard. If you include it twice and look at the preprocessed output, you see there's no sign for the second inclusion. However, if you include it twice - once from a relative path, and once from an absolute one - you see that the second inclusion indeed occurs (enters the file and leaves immediately due to the include guard). Why does this happen (I have my speculations, but I want some...
6
3907
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: // SomeClass.hh #ifndef SOME_CLASS_HH #define SOME_CLASS_HH class SomeClass {
9
1727
by: ramsatishv | last post by:
Hi, If I include a ".h" file for multiple times, will it increase my program size?? Regards Ram.
0
8773
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9445
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.