Sign in to follow this  
Alexistran

Global vars in header file

Recommended Posts

I'm in the process of creating a simple text RPG game and I'm now trying to clean up some of my code by separating the source into more files. In the process of trying to split 'battle.cpp' the file containing all functions related to (you probably guessed it) battling into more files I moved all the global variables and prototypes to a new header file, however these global variables were not constant and were declared and defined at the same time
#ifndef BATTLE_DEF
#define BATTLE_DEF
int bonushit, bonushealth, bonusmana, bonusevade, bonusdmg, bonusspelldmga = 0;
int bonusdmgtype, bonusspelldmgbytype = -1;
float bonuscost = 10;
int bonusspelldmgm = 100;
int defreturn = 0;
#endif




However when I try and compile I get told that:
>battle_phases.obj : error LNK2005: "int bonusdmgtype" (?bonusdmgtype@@3HA) already defined in battle.obj
2>battle_phases.obj : error LNK2005: "int bonushealth" (?bonushealth@@3HA) already defined in battle.obj
2>battle_phases.obj : error LNK2005: "bool scanned" (?scanned@@3_NA)
 already defined in battle.obj
2>battle_phases.obj : error LNK2005: "int bonusevade" (?bonusevade@@3HA) already defined in battle.obj
2>battle_phases.obj : error LNK2005: "int bonusdmg" (?bonusdmg@@3HA) already 
defined in battle.obj
2>battle_phases.obj : error LNK2005: "int bonushit" (?bonushit@@3HA) already
 defined in battle.obj
2>battle_phases.obj : error LNK2005: "int bonusmana" (?bonusmana@@3HA) already defined in battle.obj
2>battle_phases.obj : error LNK2005: "int bonusspelldmga" (?bonusspelldmga@@3HA)
 already defined in battle.obj
2>battle_phases.obj : error LNK2005: "int bonusspelldmgbytype" (?bonusspelldmgbytype@@3HA) 
already defined in battle.obj
2>battle_phases.obj : error LNK2005: "float bonuscost" (?bonuscost@@3MA) 
already defined in battle.obj
2>battle_phases.obj : error LNK2005: "int bonusspelldmgm" (?bonusspelldmgm@@3HA)
 already defined in battle.obj
2>battle_phases.obj : error LNK2005: "int defreturn" (?defreturn@@3HA) already defined in battle.obj
Any ideas what I'm doing wrong?

Share this post


Link to post
Share on other sites
Yup. When you compile, each .cpp is compiled independently, to create object files (usually .obj). These files are then linked together by the linker, and any references from one object file to a variable, function, and so forth that is not in the same object file is linked to the object file that does contain it.

Your problem is with putting the definition in a header file. Every .cpp that includes that header file ends up defining that variable (#ifdef/#define/#endif inclusion guards only ensure that you don't declare things multiple times per translation unit, i.e., .cpp file). Thus, the variable is located in multiple object files, and the linker doesn't know which one to use.

The solution: Use the extern keyword in front of the variable type/name in the header file, and leave out the initial value. In any .cpp file that includes the header (but only one .cpp), put what you currently have in the header file in there (no extern keyword, but with initial value, if you don't want 0). You can even make a new .cpp file if you want. Call it globals.cpp or something. Just make sure that the definition only occurs in one single translation unit, so that the linker doesn't get confused by multiple identical looking variables/functions.

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