Hi,
The problem is:
Header A-> include B
Header B-> include C
Header B-> include A
Header C-> include A
All the header have a pointer to struct that reside in include header.
How can i resolve this problem with preprocessor directivies?
Thanx
Hi,
The problem is:
Header A-> include B
Header B-> include C
Header B-> include A
Header C-> include A
All the header have a pointer to struct that reside in include header.
How can i resolve this problem with preprocessor directivies?
Thanx
1. redesign them. put everything into one header file.
2. use preprocessor directives to insure the header is included only once.
Code:#if defined(_HEADER_A_H) #define _HEADER_A_H // forward declarations here struct xyz; // this is in another header file // rest of header file here // blabla #endif
Ancient Dragon, close, but not quite:
Remember, you want to compile the following code if it has not been compiled.Code:#if !defined(_HEADER_A_H) #define _HEADER_A_H // forward declarations here struct xyz; // this is in another header file // rest of header file here // blabla #endif
Look at the standard header files for examples. I usually use ifndef:
Code:#ifndef FILENAME_H #define FILENAME_H 1 /* stuff in header file that can only be included once */ #endif
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Me too. For me it's a nice little mnemonic because ifdef and define have the same number of characters. In my editor if the two lines for inclusion guards don't line up, that sets off a warning flag in my mind and I usually need to change ifdef to ifndef to fix it.I usually use ifndef:
Just on a side note since no one has mentioned it yet, identifiers leading underscores followed by an upper case letter are always reserved by the implementation. So it's possible that mysterious breakage could happen with Ancient Dragon's example.
Just because I don't care doesn't mean I don't understand.
if you use !defined(...) you can have more than one expresssion
You can't do something like above with #ifdef statementsCode:#if defined(_MSVC) || defined(_BORLAND) || defined(_GCC) // blabla #elif defined(_UNIX) && !defined(_X11) // blabla #elif defined(_UNIX) && !defined(_MAC) // blabla #endif
> identifiers leading underscores followed by an upper case letter are always reserved by the implementation
I often ignore rules like that. That's just a guideline, not a rule that is enforced by any compiler.
Last edited by Ancient Dragon; 09-10-2005 at 03:13 PM.
Which is irrelevant with inclusion guards because you only need one unique identifier per header. In general I agree that defined() is more flexible, but for inclusion guards I don't see the need.if you use !defined(...) you can have more than one expresssion
But that wasn't a multiple inclusion guard, which is what I was talking about since that's what the topic of the thread is.You can't do something like above with #ifdef statements
It doesn't have to be enforced. It'll just break silently and with strange behavior. You'll be searching for days to find a bug like that. Ignore it if you'd like, but I've been bitten by that particular bug before, and it's not pretty.That's just a guideline, not a rule that is enforced by any compiler.
Just because I don't care doesn't mean I don't understand.