Jump to content
  • Advertisement

Archived

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

AndreTheGiant

global variable that is self contanied?

This topic is 5652 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 know you can have a global.h file, and include it in all of your source files that need the functions and variables defined within global.h, but I cant figure out a way to make it more self-contained. For example, I want a global variable that i can use for debugging which outputs info to a file. I currently have a global ofstream object called debugOut. I have this line: extern ofstream debugOut; in global.h, so that each file in which I include global.h, I can use the debugOut variable like this for example:
  
debugOut<<"ball at position: "<<ballList[i].position.print()<<endl;
   
Now the problem is that, for this to work, I need to define the following in my main.cpp: ofstream debugOut("debug.txt", ios::out); While this might not seam like a big deal, I really want everything to be more self contained. For example, when I start a new project, I will likely still use this debug variable because I like the idea. But I dont want to have to have the above line in all my main.cpp files i ever make. I want it to be more self contained. I want to just be able to include a header and then use the debugOut variable, you know what I mean? I hope Im asking this clearly enough.. Thanks! [edited by - AndreTheGiant on March 31, 2003 10:55:24 AM] [edited by - AndreTheGiant on March 31, 2003 10:55:57 AM]

Share this post


Link to post
Share on other sites
Advertisement
make debug.h with extern ostream debugOut, make debug.cpp with ostream debugOut("debugOut.txt",.....);, and include them both in your project.

when ever you want to use debugOut, just add debug.h and debug.cpp into your project, and include debug.h, and use it

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
Andre, if you use just the global.h as you have it now, the debugOut variable is declared as extern. What that means is that the variable hasn't actually been declared anywhere, so you need to find someplace to declare it, either in your main.cpp, or in another cpp. (extern means to the compiler "this variable exists somewhere and you'll find out where at link time"). It allows a variable to be visible to one object cpp file even though it's actually declared in another cpp file. If you remove the extern in global.h, then you will get multiple declarations, because every file that includes global.h will declare the debugOut variable. This will cause a compilation error because variables can only be declared once.

So, either you declare it in main.cpp, or you declare it in another cpp. You could compile the global.cpp file separately into a lib and just link it into your projects, or you could actually add the global.cpp file to each project.

So, to make it all very clear:

in global.h:
extern ofstream debugOut;

in global.cpp:
ofstream debugOut;

In each file in your project that uses debugOut, inlude global.h. If global.cpp was compiled as a lib, link to that lib when compiling, otherwise, add global.cpp to your project.

[edited by - fizban75 on March 31, 2003 12:52:50 PM]

Share this post


Link to post
Share on other sites
In answer to his real question about "making it more self-contained". One simple way is to implement this as a class and providing an accessor to the variable via a static method or variable:

in global.h:

class Debug {
public:
static ofstream log ...
};

Of course this means you have to either: a) rename the global variable you use everywhere to "Debug.log" or b) provide a preprocessor macro in your header file:

#define dbgOut Debug.log

Regards,
Jeff

Share this post


Link to post
Share on other sites
There are two standard ways to get around having to have a global.cpp or somesuch.

The first is to have a magic #define that removes the extern. In one file set the #define before including global.h. This marginally better than just defining the variable yourself in main.cpp because you can do a bunch of them in one shot.

The second way only works in you''re using VC. VC lets you put the attribute __declspec(selectany) on data. The basically tells the linker to merge all the definitions of that piece of data it sees. Just use __declspec(selectany) in place of extern.

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!