Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


'#ifndef' how is this working?

This topic is 5647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok, i should know this by now, but i dont haha, i got a tutorial on how to display multiple bitmaps and i dunno how all the files are linked together. i would always add "#include 'whatever.cpp'" in all of my files if i had more than one. but they have this... 'displaybitmaps.cpp' #include "bitmap.h" 'displaybitmaps.h' #ifndef _BITMAP_H #define _BITMAP_H 'bitmap.cpp' #include "bitmap.h" 'bitmap.h' #ifndef _BITMAP_H #define _BITMAP_H now these are all of the defines and includes(except windows.h and such). so how are all of these files linked together? i looked all over msdn and couldnt figure it out. [edited by - when4 on January 30, 2003 3:30:40 PM]

Share this post

Link to post
Share on other sites
In pseudo it''s like this

if not defined _BITMAP_H {
define _BITMAP_H
//some stuff here


//now if someone tested again for "if not defined _BITMAP_H" it would be defined and the code would be skipped

Which makes the "some stuff" to be only once in the code.
So in case someone does this:

#include "bitmap.h"
#include "bitmap.h"
#include "bitmap.h"

The contents of bitmap.h will only be included once and not 3 times.

I don''t know why displaybitmaps.h has the same defines as bitmap.h (_BITMAP_H). That''s probably a typo. The defines should be different, otherwise if you do this:

#include "bitmap.h"
#include "displaybitmaps.h"

it will only include what''s inside bitmap.h, not what''s inside displaybitmaps.h (since by the time it reaches displaybitmaps.h, _BITMAP_H is already defined). And simply changing the order

#include "displaybitmaps.h"
#include "bitmap.h"

Will make only displaybitmaps.h''s contents to be included. This is most likely undesirable.

By the way, there''s also #endif at the end of your header files. That''s the same as ''}''-character for normal if clause.

Share this post

Link to post
Share on other sites
Well, I'm not sure if that was your question but I use them like this:

(Creating some .cpp files that both need the same include)

main.cpp (edited by user)
engine.cpp (holds engine)

the header file that's included in both is engine.h (this is needed in main.cpp for the functions of this fake engine)

If you include the header in both files you get errors, but if you make the header like this:

#ifndef _ENGINE_H_ // (may have another name, just choose a name that doesn't give problems)
#define _ENGINE_H_

// Do all your header stuff


This will make sure that variabeles are not defined twice if the header is included in both files, so you don't get the errors. But what's the use then? Well, I don't know how, but somehow the .cpp file where the header is included second (so the last called file) also reads in the variabele names and functions etc, so you can still use them in your .cpp file

Well there's a little more to it than that I think, but that's it (btw #ifndef means "if not defined" or something )

Sorry for saying things twice unnecessarily... the "post" page didn't work for a few times...

[edited by - Subotron on January 30, 2003 3:02:55 PM]

Share this post

Link to post
Share on other sites
yes, thats what i wanted to know Subotron.
so its just a way to include a file multiple times.

but also, when i use multiple files in a program i write...

#include "Bitmaps.cpp"

#include "Functions.cpp" //or whatever other files i would have.

and so on, so how is ''bitmaps.cpp'' included into the program?

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!