Using same variable in Header and Source file

Thread Starter

karthi keyan 9

Joined Apr 17, 2018
30
Hai,
I am using PIC16F development board, Mplab X IDE v4.20 and XC8 compiler.
In source file
range=1;
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:
_range(dist/default/production\Digas.X.production.obj)
(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 :)
 

Picbuster

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

do not forget your proto types and includes
examples

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

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

Picbuster
 

george4657

Joined Apr 12, 2016
15
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.

George
 

Tesla23

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

<header.h>
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

<source.c>
int range = 1;

that's all there is to it.

If you did your other suggestion and put

<header.h>
int range = 1;

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