Use of Extern for sharing veriables

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 ???
..
}

Parents
  • 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.

Reply
  • 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.

Children
More questions in this forum