Archived

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

Including globals in .h ONCE

This topic is 5824 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

I''m trying to code a little DirectX, but this isn''t that a specific question. But my problem is this. I have global variabels. And I''m using C++ with classes, some class functions use these global variables. For now I have 2 classes (Sprite and DirectX setup, both in separate .h and .cpp files. I''m having HUGE problems declaring these simple global variables/constants. If I try in main.cpp they won''t be declared when it enters the #include''s at the top. So I tried making a globals.h and put all globals there. And I did #ifndef - #define - #endif in it too. And then included globals.h in the other .h''s. Now I get a million linker errors saying that they''re already defined! What is the simplest/best way to declare globals that are accesable EVERYWHERE (all .h, .cpp, .whatever)? thanx... }+TITANIUM+{

Share this post


Link to post
Share on other sites
In your globals.h put ''extern'' in front of all declarations, that tells the compiler that these vars are defined somewhere else in the program and that it is just fine to use them.
Then in some .cpp file put the actual declarations without ''extern''.

Share this post


Link to post
Share on other sites