duplicate includes

Started by
4 comments, last by CoolnessItself 19 years, 11 months ago
blah.h contains some global stuff i want to use in a few classes

#ifndef _BLAH_H
#define _BLAH_H
#pragma once //should be the same as above (i think)

#include "CSettings.h" //class containing CSettings (see below)

#include "CLog.h" //similar, but this is for CLog

CSettings theSettings;
CLog theLog;
bool bActive = true;
#endif
then i have 2 files (init.cpp and main.cpp) which need to use theSettings and theLog. Each one has something like
#include "blah.h"
but im getting the following on compile: Init.obj : error LNK2005: "bool bActive" (?bActive@@3_NA) already defined in main.obj Init.obj : error LNK2005: "class CSettings theSettings" (?theSettings@@3VCSettings@@A) already defined in main.obj Init.obj : error LNK2005: "class CLog theLog" (?theLog@@3VCLog@@A) already defined in main.obj but I thought the pragma once and/or the #ifndef...define...endif would stop this from happening. What am I missing?
Advertisement
you should instanciate your global in a cpp file
and in your blah.h extern them :

extern CSettings theSettings;

--
SharkOne
-- SharkOne
The header guards stop the header from being included twice in each translation unit, but you have 2 translation units in your case, which means you have 2 definitions of the same variable, violating the one definition rule.

The solution is to declare them in the header file, and define them in ONE translation unit.

#ifndef _BLAH_H#define _BLAH_H#include "CSettings.h" #include "CLog.h" extern CSettings theSettings;extern CLog theLog;extern bool bActive;#endif//my1.cppCSettings theSettings;CLog theLog;bool bActive = true;


gamedev article on this
Declarations go in header files. Definitions , however, need to go in a source file. Your variables listed are definitions. What you would typically do is also have a blah.cpp file. blah.cpp would look like:
#include "blah.h"CSettings theSettings;CLog theLog;bool bActive = true;
And blah.h would look like:
#ifndef _BLAH_H#define _BLAH_H#include "CSettings.h" //class containing CSettings (see below)#include "CLog.h" //similar, but this is for CLogextern CSettings theSettings;extern CLog theLog;extern bool bActive;#endif
[edit - darn, beaten by two people, as usual... At least I take up more space! ]
[edit - my edit maliciously removed closing/opening source tags...]

[edited by - Agony on May 19, 2004 6:02:02 PM]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
I think

extern bool bActive = true;


is also a definition Agony . You can''t put initializers on a declaration, otherwise it becomes a definition, and will give you a linker error again.
quote:Original post by Jingo
I think
extern bool bActive = true; 


is also a definition Agony . You can''t put initializers on a declaration, otherwise it becomes a definition, and will give you a linker error again.
Whoops, my mistake indeed, although I blame it on rushed copy/pasting, which might have been obvious. I shall edit it.

"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

This topic is closed to new replies.

Advertisement