Advertisement

??? already defined....

Started by November 20, 2000 10:52 PM
4 comments, last by dbrooking 24 years, 2 months ago
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.:
  #ifndef G_GLOBALS_H_#define G_GLOBALS_H_...code here...#endif  


------------------------------
BCB DX Library - RAD C++ Game development for BCB
Advertisement
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
ECKILLER
I do have header guards in the files

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
Regards,JumpsterSemper Fi
That solved the problem Thanks.

This topic is closed to new replies.

Advertisement