We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi,
I defined,
Uint16 TCounts[10]; in globals.h
and tried to use it in cc01drv.c where #include "globals.h" is there.
but during build the error msg coming up is, "C202 undefined identifier"
Why this is happening, I haven't initialize the array.
You don't need to define any _MAIN_.
You have found some sample program where a specific developer have selected to use a specific way to write code. That doesn't mean that a general purpose book will have info about this specific method.
It isn't part of the preprocessor but part of the compiler to keep track of your definitions and declarations.
You can write in one (actually many header files, but you should only write it in one) header file:
extern volatile Uint16 xdata PCounts;
That means that any C file that includes this header fille will know about the existence of PCounts, and what type it takes.
You can then write in one (exactly one) C file:
volatile Uint16 xdata PCounts;
This since line is the one that will make the linker reserve room for the variable.
For maximum security, you should make sure that this since C file also includes the header file that contains the "extern" line, so the compiler can catch if you have managed any type errors between the two lines.
The use of the preprocessor with _MAIN_ is just some form of syntactic sugar, where a developer has moved both lines into the header file, and then uses the _MAIN_ symbol to get the preprocessor to extract the extern declarations for all source files but the single file where _MAIN_ is defined. And the source file with _MAIN_ defined (possibly a file named main.c) will instead get the line that doesn't contain "extern".
It would have been better if the code had been written:
my_header.h:
extern volatile Uint16 xdata PCounts; #ifdef _MAIN_ volatile Uint16 xdata PCounts; #endif
and main.c:
#define _MAIN_ #include "my_header.h" ...
and other source files:
#include "my_header.h" ...
The use of #else in your example means that the compiler will not catch type errors.