Sign in to follow this  
zix99

Linking problems...

Recommended Posts

I'm making an OpenGL application and i start getting linking errors suddenly. I didnt change anything (purposely) ERRORS: main.obj : error LNK2005: "class ATMOSPHERECLASS atmosphere" (?atmosphere@@3VATMOSPHERECLASS@@A) already defined in atmosphere.obj main.obj : error LNK2005: "float mspeed" (?mspeed@@3MA) already defined in atmosphere.obj main.obj : error LNK2005: "long worldtime" (?worldtime@@3JA) already defined in atmosphere.obj main.obj : error LNK2005: "int ground" (?ground@@3HA) already defined in atmosphere.obj main.obj : error LNK2005: "long lastframe" (?lastframe@@3JA) already defined in atmosphere.obj textures.obj : error LNK2005: "class ATMOSPHERECLASS atmosphere" (?atmosphere@@3VATMOSPHERECLASS@@A) already defined in atmosphere.obj textures.obj : error LNK2005: "float mspeed" (?mspeed@@3MA) already defined in atmosphere.obj textures.obj : error LNK2005: "long worldtime" (?worldtime@@3JA) already defined in atmosphere.obj textures.obj : error LNK2005: "int ground" (?ground@@3HA) already defined in atmosphere.obj textures.obj : error LNK2005: "long lastframe" (?lastframe@@3JA) already defined in atmosphere.obj Debug/atmosphere.exe : fatal error LNK1169: one or more multiply defined symbols found It compiles atmosphere.cpp, then main.cpp, then textures.cpp. The code for these variables is in the header file 'std.h' and should work (seeing as it worked fine at one point in time, but now it doesnt) (sorry i dont know how to post code properly, i'm to new to these forums) -------------------------------------------- //global variables #ifndef _GLOBAL_VARS_ #define _GLOBAL_VARS_ ATMOSPHERECLASS atmosphere; long worldtime; float mspeed; long lastframe; int ground; #endif -------------------------------------------- Thanks for any information, i'm clueless. -zix

Share this post


Link to post
Share on other sites
Define global variables in a .cpp file. Use extern in a global header. For instance, you could create std.cpp with the following.


ATMOSPHERECLASS atmosphere;
long worldtime;
float mspeed;
long lastframe;

int ground;


and std.h would have the following.


extern ATMOSPHERECLASS atmosphere;
extern long worldtime;
extern float mspeed;
extern long lastframe;

extern int ground;


Extern declares that a variable of that name and type exists in another file somewhere. What was happening was that every file including std.h was redefining all of the global variables. Using extern and declaring the globals in a .cpp file will fix that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this