You can not declare objects in a header file without the extern keyword. When you do it without that you will get the results you have because the objects will be defined in every *.cpp file in which the header file is included. Use extern in header files. Then in one, and only one *.c or *.cpp file declare them again but without extern.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
Inclusion protection preprocessor symbols like this are good, but they should go inside the file they are protecting not round every place it is included. Common_Definitions_h should have the structure
#ifndef Common_Definitions_h
#define Common_Definitions_h
... declarations and inclusion of other files here
#endif
and then you just included it like this
#include "Common_Definitions.h"
Your link errors are a rresult of the contents of Common_Definitions.h1) Common_Definitions.h
unsigned char X1[16];
char X2[4];
struct xyz
{
UINT32 X3;
unsigned int X4[4];
long X5;
int X6;
int X7;
};
You are defining X1 and X2 when you should be declaring them. A definition basically causes the compiler to create an object, in this case the are variables so it reserves data for them. A declaration informas the compiler that the object exists somewhere and that it doesn't need to create it it can just use it.
When you define data in a header file and that header is included into multiple C files every object file produced by the compiler has space reserved for that variable. Then when the linker trys to link all the object files it finds that the variable is defined in multiple locations and since it has no way of telling what to do sensibly in that situartion it stops with a "multiple definition" or "already defined" error.
A data declaration looks like this extern unsigned char X1[16]; that is what should be in your header file, then in 1 cpp file you declare the data with extern unsigned char X1[16]; .
That way all cpp files can see the declaration and use the data but it is only defined in one file so there is only one definition of the data and the linker doesn't get confused.
BTW you should avoid using global data if at all possible, it leads to bugs and maintenance problems.
Banfa
Practically a Master Poster
600 posts since Mar 2010
Reputation Points: 486
Solved Threads: 92
But I am not using any objects.
It's an ambiguous use of the term object (that we all do).
In an object orientated programming sense an object is an instantiation of a class. However in the context of any executable code an object is any thing that takes up space in memory such as a char array.
Banfa
Practically a Master Poster
600 posts since Mar 2010
Reputation Points: 486
Solved Threads: 92
This one?
1>d:\...\common_deFINITIONS.h(18) : error C2086: 'unsigned char X1[16]' : redefinition
This happens when the compiler sees 2 definitions of the same object (not the OOP type) such as when you define something in a header file that doesn't use multiple inclusion protection via #defines and then included it multiple times directly or indirectly into a single cpp file.
Banfa
Practically a Master Poster
600 posts since Mar 2010
Reputation Points: 486
Solved Threads: 92