global variable that is self contanied?

Started by
7 comments, last by AndreTheGiant 21 years ago
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]
Advertisement
Create a global.cpp file and put it there, then just include that file in all your projects.
How is that different from creating a global.h file and including it in all my projects? I guess I must not understand what you mean.
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
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

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]
debugOut.open( "...", ... );

.lick
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
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.
-Mike
To everyone and noone in particular : that''s what libraries - static in this case - are for.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement