Archived

This topic is now archived and is closed to further replies.

CoolnessItself

duplicate includes

Recommended Posts

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?

Share this post


Link to post
Share on other sites
you should instanciate your global in a cpp file
and in your blah.h extern them :

extern CSettings theSettings;

--
SharkOne

Share this post


Link to post
Share on other sites
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.cpp

CSettings theSettings;
CLog theLog;
bool bActive = true;


gamedev article on this

Share this post


Link to post
Share on other sites
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 CLog

extern 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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites