Take the 2-minute tour ×

I have a file:


#ifndef _variableinclude_h_
#define _variableinclude_h_

AClass* variable1;
int* variable2;


But I include this file in another two different ones:

- atest1.h

- atest2.h

The problem is the following: variable redefinition.

How to avoid that???

share|improve this question
you are getting linker error or a compiler error? –  Naveen Oct 1 '10 at 5:04
Note that the include guard is illegally using a reserved prefix (_v). Also, by convention macro's are uppercase. –  MSalters Oct 1 '10 at 7:26
@MSalters: Why is this reserved? I thought only double underscores shouldn't be used. –  ur. Oct 1 '10 at 7:29
@ur: Double underscores are reserved everywhere in tokens, not just as a prefix. And they're reserved for unspecified use by the implementation. E.g. a compiler may internally define _variableinclude_h_ as soon as you #include <variableinclude.h>. –  MSalters Oct 1 '10 at 7:48
Any identifier containing a double underscore is reserved. Anything starting with underscore followed by a capital letter is reserved. Anything else starting with underscore is reserved if it is declared at namespace scope (which your include guard is) –  jalf Oct 1 '10 at 9:49
add comment

1 Answer


Welcome to ODR


Make the variables extern in the header file.

extern AClass* variable1;   // assuming AClass is declared at this point.
extern int* variable2;

Define them once and only once in any cpp file e.g. in main.cpp at namespace scope.

AClass* variable1 = NULL;   // assuming AClass is declared at this point.
int* variable2 = NULL;
share|improve this answer
+1 the solution –  Mario The Spoon Oct 1 '10 at 5:05
add comment

Your Answer


