#ifndef problems 
Author Message
 #ifndef problems

In many include files are the lines:
#ifndef GLOBAL_H
#define GLOBAL_H

...content of include file

#endif

This is supposed to keep the preprocessor from loading definitions if they
have been already loaded.  This does not seem to be working in Visual C++
6.0 (sp5).  Can anyone shed any light on this for me?

Thanx

Richard Haber



Sun, 27 Jun 2004 04:01:03 GMT  
 #ifndef problems
I have VC++ 6.0 SP5 and it works for me. What error is being reported?
--
Randy Charles Morin
http://www.kbcafe.com

Feel free to contact me by private email or messenger


Quote:

> In many include files are the lines:
> #ifndef GLOBAL_H
> #define GLOBAL_H

> ...content of include file

> #endif

> This is supposed to keep the preprocessor from loading definitions if they
> have been already loaded.  This does not seem to be working in Visual C++
> 6.0 (sp5).  Can anyone shed any light on this for me?

> Thanx

> Richard Haber



Sun, 27 Jun 2004 10:25:06 GMT  
 #ifndef problems
Note that include guards (as these are commonly called) only prevent a
header file from being processed multiple times from one .cpp file. The
header may still be processed multiple times per project, if different
.cpp files include it. So, having variable definitions inside a header file
is not safe, include guards or not.

Also, include guards only work if they are unique. Defines like GLOBAL_H
can easily overlap.

Include guards do work - but only if you use them properly.

Quote:

> In many include files are the lines:
> #ifndef GLOBAL_H
> #define GLOBAL_H

> ...content of include file

> #endif

> This is supposed to keep the preprocessor from loading definitions if they
> have been already loaded.  This does not seem to be working in Visual C++
> 6.0 (sp5).  Can anyone shed any light on this for me?

> Thanx

> Richard Haber



Sun, 27 Jun 2004 14:23:52 GMT  
 #ifndef problems
Bruce,

Thanks for your post.  The amount of valuable things I learn from people as
yourself on even the most-taken-for-granted topics never ceases to amaze me!

Thanks again,

Todd


Quote:
> Note that include guards (as these are commonly called) only prevent a
> header file from being processed multiple times from one .cpp file. The
> header may still be processed multiple times per project, if different
> .cpp files include it. So, having variable definitions inside a header
file
> is not safe, include guards or not.

> Also, include guards only work if they are unique. Defines like GLOBAL_H
> can easily overlap.

> Include guards do work - but only if you use them properly.


> > In many include files are the lines:
> > #ifndef GLOBAL_H
> > #define GLOBAL_H

> > ...content of include file

> > #endif

> > This is supposed to keep the preprocessor from loading definitions if
they
> > have been already loaded.  This does not seem to be working in Visual
C++
> > 6.0 (sp5).  Can anyone shed any light on this for me?

> > Thanx

> > Richard Haber



Mon, 28 Jun 2004 02:08:35 GMT  
 #ifndef problems
i agree no matter how many books i read i was always under the impression
they had said "to avoid multiple inclusions in large setup use the
following: and then did the infdef define endif
so i was always amazed how i as the one person in the univers cant include
it in two places w/i the linker {*filter*}ing....

i managed to change the code now to where i simply only include it once in
the project and create the object in the file that makes use of my class
but it really amazes me how it took me to lurk in a NG to find out why it
didnt work.........it never was supposed to work in a project with more than
one .cpp file

thanks!

Quote:
> Bruce,

> Thanks for your post.  The amount of valuable things I learn from people
as
> yourself on even the most-taken-for-granted topics never ceases to amaze
me!

> Thanks again,

> Todd



> > Note that include guards (as these are commonly called) only prevent a
> > header file from being processed multiple times from one .cpp file. The
> > header may still be processed multiple times per project, if different
> > .cpp files include it. So, having variable definitions inside a header
> file
> > is not safe, include guards or not.

> > Also, include guards only work if they are unique. Defines like GLOBAL_H
> > can easily overlap.

> > Include guards do work - but only if you use them properly.


> > > In many include files are the lines:
> > > #ifndef GLOBAL_H
> > > #define GLOBAL_H

> > > ...content of include file

> > > #endif

> > > This is supposed to keep the preprocessor from loading definitions if
> they
> > > have been already loaded.  This does not seem to be working in Visual
> C++
> > > 6.0 (sp5).  Can anyone shed any light on this for me?

> > > Thanx

> > > Richard Haber



Mon, 28 Jun 2004 03:32:48 GMT  
 #ifndef problems
It is supposed to work on projects with more than one .cpp file, and it does
avoid multiple declarations. But it was never intended to avoid multiple
definitions.

i.e.; it works famously if your header file contains:

class Foo
{
    int x;

Quote:
};

but it works badly if your header file contains:

int x;

Each .cpp file is a separate compilation unit (C++ standard language). Once
you understand that then the behavior of the include guards makes perfect
sense.

Quote:

> i agree no matter how many books i read i was always under the impression
> they had said "to avoid multiple inclusions in large setup use the
> following: and then did the infdef define endif
> so i was always amazed how i as the one person in the univers cant include
> it in two places w/i the linker {*filter*}ing....

> i managed to change the code now to where i simply only include it once in
> the project and create the object in the file that makes use of my class
> but it really amazes me how it took me to lurk in a NG to find out why it
> didnt work.........it never was supposed to work in a project with more than
> one .cpp file

> thanks!


> > Bruce,

> > Thanks for your post.  The amount of valuable things I learn from people
> as
> > yourself on even the most-taken-for-granted topics never ceases to amaze
> me!

> > Thanks again,

> > Todd



> > > Note that include guards (as these are commonly called) only prevent a
> > > header file from being processed multiple times from one .cpp file. The
> > > header may still be processed multiple times per project, if different
> > > .cpp files include it. So, having variable definitions inside a header
> > file
> > > is not safe, include guards or not.

> > > Also, include guards only work if they are unique. Defines like GLOBAL_H
> > > can easily overlap.

> > > Include guards do work - but only if you use them properly.


> > > > In many include files are the lines:
> > > > #ifndef GLOBAL_H
> > > > #define GLOBAL_H

> > > > ...content of include file

> > > > #endif

> > > > This is supposed to keep the preprocessor from loading definitions if
> > they
> > > > have been already loaded.  This does not seem to be working in Visual
> > C++
> > > > 6.0 (sp5).  Can anyone shed any light on this for me?

> > > > Thanx

> > > > Richard Haber



Mon, 28 Jun 2004 13:08:34 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. #ifndef/#ifdef problems in my header file (conditional header inclusion)

2. #ifndef... #endif problem

3. #pragma once and #ifndef/#endif

4. linking + #ifndef

5. #ifndef error

6. #ifndef not working?

7. #ifdef acting like #ifndef ?

8. ifndef/define/endif

9. use of #ifndef...

10. use of #ifndef machinetype

11. Does #ifndef really keep code from being compiled

12. #ifndef/#define doesn't work in ATL header

 

 
Powered by phpBB® Forum Software