error LNK2005...already defined
While building my code in VS2008 I am getting a build error:
error LNK2005...already defined in ....
1>Linking...
1>Main.obj : error LNK2005: "unsigned char * X1" (?X1@@3PAEA) already defined in BC.obj
1>Main.obj : error LNK2005: "char * X2" (?X2@@3PADA) already defined in BC.obj
1>Rs.obj : error LNK2005: "unsigned char * X1" (?X1@@3PAEA) already defined in BC.obj
1>Rs.obj : error LNK2005: "char * X2" (?X2@@3PADA) already defined in BC.obj
In my project I have three .h files:
1) Common_Definitions.h
unsigned char X1[16];
char X2[4];
struct xyz
{
UINT32 X3;
unsigned int X4[4];
long X5;
int X6;
int X7;
};
2) BC.h
3) RS.h
I have three .cpp files
1) Main.cpp
2) BC.cpp
3) RS.cpp
In Common_Definitions.h I have definitions/declarations which I want to be available in entire project, While BC.h and RS.h has corncenred variables.
I have included Common_Definitions.h in BC.h and RS.h.
#ifndef Common_Definitions_h
#define Common_Definitions_h
#include "Common_Definitions.h"
#endif
I have included BC.h and RS.h in their corresponding .cpp files.
I have included BC.h and RS.h also in Main.cpp
Now while building I get the linker error KNK2005.
How to resolve it?
Earlier when I had included Common_Definitions.h in BC.h and RS.h directly without ifndef.
#include "Common_Definitions.h"
then it was giving compilation error:
1>d:\...\common_deFINITIONS.h(18) : error C2086: 'unsigned char X1[16]' : redefinition
1> d:\...\common_deFINITIONS.h(18) : see declaration of 'X1'
1>d:\...\common_deFINITIONS.h(19) : error C2086: 'char X2[4]' : redefinition
1> d:\...\common_deFINITIONS.h(19) : see declaration of 'X2'
1>dd:\...\common_deFINITIONS.h(22) : error C2011: 'XYZ' : 'struct' type redefinition
1> d:\...\common_deFINITIONS.h(22) : see declaration of 'XYZ'
So to resolve that I have included Common_Definitions.h in BC.h and RS.h using ifndef.
#ifndef Common_Definitions_h
#define Common_Definitions_h
#include "Common_Definitions.h"
#endif
But this is leading to linker error.
Please let me know how to resolve this compiler and linker issue?