??? already defined....
Alright. I''ve been having this problem for a while now but jsut now it''s really starting to bother me cuz I see other people do it all the time. I have a header file called gl_globals.h. In this file I have global variables all declared ''extern''. When I include it in just one .cpp file, everything works ok. But when I include it in more than one file, I get linker errors. I know you can do this because I have seen it done before but for some reason I can''t get it to work. Any suggestions?
Have you declared header guards? i.e.:
------------------------------
BCB DX Library - RAD C++ Game development for BCB
#ifndef G_GLOBALS_H_#define G_GLOBALS_H_...code here...#endif
------------------------------
BCB DX Library - RAD C++ Game development for BCB
h file
if not defined i
define i
extern int i;
end if
cpp file 1
include whatever
extern int i = 0;
cpp file 2
include whatever
extern int i;
The trick is to only initialize an extern once, in one cpp file
ECKILLER
if not defined i
define i
extern int i;
end if
cpp file 1
include whatever
extern int i = 0;
cpp file 2
include whatever
extern int i;
The trick is to only initialize an extern once, in one cpp file
ECKILLER
ECKILLER
I do have header guards in the files
this is what i have
That is gl_globals.h. When I include it in more than one .cpp file, I get the errors.
this is what i have
#ifndef GL_GLOBALS_H_#define GL_GLOBALS_H_extern bool active;extern bool fullscreen;#endif
That is gl_globals.h. When I include it in more than one .cpp file, I get the errors.
The globals.h file declares them as extern... that''s good. Which CPP file actually declares them? You only need one cpp to declare them in, not all CPPs that will use them.
Example: globals.cpp
bool active = false;
bool fullscreen = true;
void DeclareVars(bool setactive, bool setfullscreen)
{
active = setactive;
fullscreen = setfullscreen;
}
Also, another thing to look for are variables that have the same name in the CPP file. To prevent this sort of thing, you may want to consider renaming the globals as such:
bool g_Active;
bool g_FullScreen;
This way, you know the letter g_ keeps the names unique.
Regards,
Jumpster
Example: globals.cpp
bool active = false;
bool fullscreen = true;
void DeclareVars(bool setactive, bool setfullscreen)
{
active = setactive;
fullscreen = setfullscreen;
}
Also, another thing to look for are variables that have the same name in the CPP file. To prevent this sort of thing, you may want to consider renaming the globals as such:
bool g_Active;
bool g_FullScreen;
This way, you know the letter g_ keeps the names unique.
Regards,
Jumpster
Regards,JumpsterSemper Fi
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement