Globals are ugly for many reasons. My advise would be to rethink your approach here.
If you really do want to use a global....
It's kind of like with functions. If you want a function available to many .cpp files, you put the
prototype in a header, but you put the actual function body in a cpp file. With variables it's kind of the same way. You put the extern declaration in a header (so many files can see it), but put the actual variable in only 1 cpp file (so it's only actually created once).
ie:
1 2 3
|
// globals.h
extern Cgicc cgi;
|
1 2 3
|
// globals.cpp
Cgicc cgi;
|
1 2
|
// any other cpp file you want to have access to the globals
#include "globals.h"
|
If you have a lot of globals and don't want to declare them all twice like that, you can use a macro trick:
1 2 3 4 5 6
|
// globals.h
#ifndef GLOBAL
#define GLOBAL extern
#endif
GLOBAL Cgicc cgi;
|
1 2 3
|
// globals.cpp
#define GLOBAL
#include "globals.h"
|
1 2
|
// all other cpp files you want the globals for
#include "globals.h"
|
This trick has 'globals.cpp' #define GLOBAL as nothing, so when you include globals.h, the variables aren't declared as extern. All other cpp files don't define GLOBAL, so when they include globals.h the variables
are declared as extern.
But again.. I have to stress that globals are a very poor design decision most of the time. If this 'cgi' variable is only going to be used in My_Class, then you're better off making it a static member:
1 2 3 4 5 6
|
// myclass.h
class My_Class
{
//...
static Cgicc cgi;
};
|
1 2 3
|
// myclass.cpp
Cgicc My_Class::cgi;
|
EDIT: doh I'm too slow. Oh well ^^