Using same variable in Header and Source file

Thread Starter

karthi keyan 9

Joined Apr 17, 2018
I am using PIC16F development board, Mplab X IDE v4.20 and XC8 compiler.
In source file
i want to check the value of 'range' in header file. for this purpose i declared like
extern int range; in header file. But it is giving error.

:0: error: (499) undefined symbol:
(908) exit status = 1

make[2]: *** [dist/default/production/Digas.X.production.hex] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)

somebody can help me on this.
Thanks :)


Joined Dec 2, 2013
in header file
int range=1;

do not forget your proto types and includes

in main #include "lcd.h" //example

//========== Proto types in .h files=====================
void Read_vars(void); //example



Joined Apr 12, 2016
defining a variable as external means it is a global variable defined in an other part of the program.
So remove "external" unless needed in other parts of the program in which case set up a global variable first.



Joined May 10, 2009
If you want a global variable 'range' that can be accessed by multiple source files, then what you do is:

extern int range;

any file that includes <header.h> now knows that there is an int variable around called 'range' that they can use. You still haven't defined where it is though, (which is why you are getting errors) so in ONE source file you put

int range = 1;

that's all there is to it.

If you did your other suggestion and put

int range = 1;

then every file that you includes <header.h> will have a separate copy of 'range'.