Hi guys,
I'm receiving the Error: 'L6200E: Symbol multiply defined'.
I understand one workaround is to include externs, however this is unideal. With most compilers/linkers something such as:
#ifndef bla #define bla
// Content
#endif
Would prevent double inclusion, however it doesn't appear to work with Keil.
Any ideas on a similar solution?
Many thanks
"I understand one workaround is to include externs"
Pardon?
extern is not a "workaround"! It is the correct way to give one file access to symbols from another - ie, to symbols which are external to it!
"however this is unideal"
How so?
"With most compilers/linkers something such as ... Would prevent double inclusion"
So-called "include guards" like this have nothing to do with Linkers.
"it doesn't appear to work with Keil"
It does work with Keil! But, of course, if your problem is not multiple-inclusion, then it won't solve it!
Include Guards prevent multiple inclusion of the header in the same translation unit - but do nothing to prevent multiple definitions due to including the same header in multiple translation units!
The solutions is to not have definitions in your headers.
See: c-faq.com/.../decldef.html
Sorry about this guys,
I'm coming at this as a person who uses C++ primarily for any project large enough to be in multiple files, where naturally I'd know straight away if I'd defined something in my header and got into the bad habit of doing it without realising.
I've just rewritten my code in C++ and now it's looking much cleaner.
Cheers
Sorry to rain on your parade, but this
clearly indicates you haven't understood the problem. This has nothing to do with C vs. C++, so moving to C++ can only have brushed the problem under the carpet, or made it a nicer-looking problem --- but it rather certainly didn't solve anything.
Your code had a plain and simple coding mistake which the compiler did tell you about right there in the original error message: multiple definitions. Get out your textbooks and look up the distinction between definitions and declarations, and you'll understand what actually did go wrong.
While you may be correct, you are also making assumptions as to what he means by "cleaned up." It could be that when he rewrote his code, it caused him to review what it was written as and he addressed a number of these issues.
I'm quite aware of what definitions mean and there was never any doubt in my mind as to the difference between this and a declaration, however having never received the error 'L6200E: Symbol multiply defined' before it wasn't immediately obvious (I'm used to symbol errors being related to library linking).
Coding in C++ there's a natural line between definitions only going in class header definitions and declarations in the accompanying cpp, generally with slightly more obvious error messages if you break from this. Likewise, I rarely find myself having to use the keyword 'extern' in an OO environment and am a bit rusty as to its use.
So what I meant by cleaned up with a C++ rewrite was that I simply followed standard C++ coding practise and it naturally solved the issue (after forum users generously highlighted the cause for me) and gave me the OO code I was wanting at the same time.
And thanks Bob for not jumping to conclusions.
That's just because you rarely use globals in C++, the same problem would have occurred in C++ if you had tried the same thing.
Instead you hand around pointers and references. The advantage is that pointers are just addresses, so declaring a class is sufficient, you don't need to know any internals to be able to create a pointer to it. References are just a different syntax for constant pointers (not pointers to constants), so the same applies to them.
View all questions in Keil forum