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.
Why can't I link it?
Because you have two compilation units each with the
You can only initialize the variable in one unit.
So, for example, put the
into main.c and change the header file to have just
int variable;
Advise that you read your C reference manual for definition, declaration and initialization.
Better, change the header file to have:
extern int variable;
"Advise that you read your C reference manual for definition, declaration and initialization"
Yes - that's the key!
Here's some 'C' reference/learning materials: blog.antronics.co.uk/.../
Better, change the header file to have:...
Yes, if he's going to make changes, he should best do it properly.
(but, I'm not sure the extern there is actually a technical requirement in C)