Hello all, I'm having problem declaring bit as global in header file to avoid including extern bit xxx in other files. I've followed Keil instruction from this link: http://www.keil.com/support/docs/1868.htm
So far all the variables are good, and I don't have to use extern type variable xxx any more. For bit I try to do this:
_DECL bit xxx _INIT(x);
but it doesn't work. Is there any way to get it through? Would it be good to use struct in this case? If so, how should I do this? Any inputs would be greatly appreciated.
Best Regards, T.L
after wasting plenty of time hunting things like char Ralph; extern short Ralph; of course, in stuff written by others :) I have become a firm believer in qqq char Ralph; where 'qqq' is defined as extern in all modules but one. Of course 'qqq' should be something meaningful - your choice.
Erik
"after wasting plenty of time hunting"
I still don't think it's proven that messing around with preprocessor tricks will waste any less time when it runs into problems - as this thread amply demonstrates!
The simple answer to your problem is to ensure that the header with the 'extern' declaration is included in the file with the real definition. That way - and only that way - the compiler can spot any discrepancies!
declaration.h
extern short Ralph; // Declaration
reference.c
#include "declaration.h" : // stuff using Ralph :
definition.c
#include "declaration.h" : char Ralph; // Definition - Compiler will warn here!
Even with preprocessor tricks, you still run the risk the the two alternatives might not match - but preprocessor trick will ensure that the compiler can never see the two and, therefore, can never report the discrepancy.
So, on balance, I rest mu case, m'lud.
I do agree that the use of 'preprocessor tricks' should not be "a hobby" and, thus I restrict my use to the #define xxx as extern or nothing, which has proven safe. Where the 'simple trick' does not work, I split like this #ifdef "the module that has the definitions" blah #else extern blah #endif
But you are still at risk from typos like
#ifdef "the module that has the definitions" signed blah #else extern unsigned blah #endif
And, because the preprocessor trick ensures that the compiler can never see both, then you are assured that it can never warn you!