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.
Because, as the error message tells you, you have multiple definitions of the symbol 'variable'.
- Extern_file.h int variable = 5;
That is almost certainly your problem!
That is a definition of the symbol 'variable'.
Therefore, every file which #includes Extern_file.h will contain a definition of the symbol 'variable'.
Therefore, if multiple (ie, more than one) files #include Extern_file.h, you will obviously have multiple definitions!!
c-faq.com/.../decldef.html
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.
youve got an error in multiplying too bit numbers and its overflowing
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/.../
"youve got an error in multiplying too bit numbers and its overflowing"
No - that has nothing to do with it whatsoever!
That's not what the "multiply" in the error message means!
It's "multiply" as in "severally" or "frequently".
It's a really poor and unhelpful way to write it!
I do wish Keil would change that - you'd have though a company from Cambridge, England would do better!
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)
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.
CONVERT YOUR VARIABLE TO DEFINE OR STOP INIT VARIABLE IN FILE "???.h" and init your variable in "???.cpp" ( or "???.c" )
I solved my problem this way
*** my english writing :)