Hi,
I'm having trouble with a global variable being defined in a header file and hen being called from two different .C files. The linker is not happy. I had Codewarrior autogenerate the MCUinit.c file and have use a global variable in one of the interrupt service routines in MCUinit.c. I'd like to use this global variable in main.c. The global is defined in a header I call extras.h, complete with guards:
// extras.h
#ifndef __EXTRAS_H
#define __EXTRAS_H
#define MAX_COUNT 10000
int Count_GLOBAL = 1;
#endif // __EXTRAS_H
I call #include "extras.h" from both MCUinit.c and main.c but the linker is telling me
Link Error: L1818: Symbol 16 - Count_GLOBAL duplicated in MCUinit.c.o and main.c.o.
When I remove the #include call in either MCUinit.c or main.c the compiler complains that the variable is not declared.
Does anyone know a way around this?
Thanks!
James
Solved! Go to Solution.
Hi James,
The header file is there to "declare" functions and variables. Nothing should ever be "defined" in a header file.
In order to change "int Count_GLOBAL = 1;" from a definition to a declaration, you need to write it like this:
extern int Count_GLOBAL;
Of course, this will leave "Count_GLOBAL" declared but undefined, so you need to put your definition in one of your .c files, probably "main.c".
Hi James,
The header file is there to "declare" functions and variables. Nothing should ever be "defined" in a header file.
In order to change "int Count_GLOBAL = 1;" from a definition to a declaration, you need to write it like this:
extern int Count_GLOBAL;
Of course, this will leave "Count_GLOBAL" declared but undefined, so you need to put your definition in one of your .c files, probably "main.c".
Hi Rocco,
We're developing a product that needs a good algorithm to calculate steps, calories, miles, sleep. Know any company or person(s) you'd recommend for the development or implementation?
Thanks
Jay
Super! Thanks Rocco!