I have a code with following files:
- Extern_file.h
int variable = 5;
void function (void);
- Extern_file.c
#include Extern_file.h
void function (void){ variable++; }
- main.c
int main(void){ function(); return 0; }
When link, I got the error:.\Objects\Blinky_LEDS.axf: Error: L6200E: Symbol variable multiply defined (by Extern_file.o and main.o). Im working over a code made from component designer (stm32F105) and migrating to nrf51822. This is only a simplified example. Why can't I link it? If I use CoIDE I don't have this problem. I have readed another link about this problem, but I can't solve it. http://www.keil.com/forum/21633/
Thanks.
I got it! If I don't initialize the variable on header file, I can build project on DevC++, but not with keil. If I put: extern int variable on Keil, I can build project.
I have to follow this metod because Im following the code of the company that programmed the chip, and I prefer to change the less code as possible.
Thank you!
If you don't initialise it, the compiler may still take it as a definition.
Some compilers will assume that duplicate definitions are intended to be the same thing, and will "merge" them. The trouble with this is that there is no way for the compiler to tell whether you did actually intend them to be the same thing, or different things.
If you put in the 'extern', then it can only ever be a declaraion - and it makes your intention explicitly clear.