I have a basic question regarding definition and declaration of variables and use of the extern attribute. I thought I understood it but may be not. Here is what I have. 3 separate files.. One of the shared variable works (Pkt), the other (LLC) does not.
I would appreciate if anyone can explain this and more important, clearly specify what needs to be done... (A headers.h file calls out subroutines such as rtn1.c so that they do link ok.)
//externals.h file extern unsigned char xdata Pkt[10]; extern unsigned char xdata LLC; extern unsigned char *k; ...
//Main.c file... variables are defined here. unsigned char xdata Pkt[10]; unsigned char xdata LLC; unsigned char *k; ... void main (void) { ... Pkt[0] = 0x01; LLC = 0x02; ... }
//subroutines.c file #include <externals.h> .. void rtn1 (void){ .. k= &Pkt[0]; // <--- No errors .. k=&LLC; // <--- Compiler says error (C202): 'LLC': undefined identifier LLC .... WHY ??? .. }
Note that your "externals.h" is a project file. So use
#include "externals.h"
The form
#include <system.h>
is for system-supplied include files where the tool chain should scan through the configured include paths to locate the file.
Next thing - remember that if a variable is stored in a specific memory region (xdata) then you want to use a pointer of the type "pointer to xdata" to access that variable. See the manual for the use of the xdata variable - especially the difference between a pointer pointing to xdata.
Another thing - no need to take the address of element zero of an array - the array name already represents an address.
I don't know about your specific error message, but start with looking at the data type of your pointer.
Hi Folks, Thanks for your reply and suggestions. I changed the #include statement to #include "externals.h" I modified the poiter *k to unsigned char xdata *k; and extern unsigned char xdata *k; in the 2 files (resp.) I knew that it was not necessary to have the [0] in front of Pkt. And some how it is working.. It seems that once the compiler finds a fatel error, it raises all kinds of errors on otherwise perfectly valid statements.
So thanks for your help.
A related condition that causes compilation problem occurs when I try to add a #include "externals.h" statement to the main.c file. It then gives an error C231: "Pkt" : redefinition. See the code snipet below. Why does that happen ? The reason I wanted to add this include stmt was, I wanted to be able to link in variables that are declared in other files. Is that not necessary in the "main.c" file, (which has some other routines also) ?? Is this error because "C" does implicit declaration of Pkt when it is first used in main.c, making the Pkt[20] declaration outside the main,c a duplicate ? is there a correct way of doing this #include in the file where main.c exists ?
I would appreciate some clarification.. Thanks. Bhal Tulpule
//Main.c file... variables are defined here. #include "externals.h" unsigned char xdata Pkt[10]; unsigned char xdata LLC; unsigned char *k; ... void main (void) { ... Pkt[0] = 0x01; LLC = 0x02; ... }