# volatile variable as global (extern volatile?)

Status
Not open for further replies.

#### moahrs

##### Junior Member level 2
Hi all... again....

I use many volatile variables type in my program, because I use interruptions and others... But now, I need use this variables in other programs as global. I need to change the content of these variables in other programs. All program are of the same project.

for sample:
volatile unsigned char pbyte;

The normal path would be to use "extern", but wont works.

In the compiler phase, its OK, but in liker phase, I get an error that, for sample, the variable "pbyte" was not found in "xyz.o"

Any1 have any idea how I solve this problem ?

Ty a lot
Moacir Jr.

- - - Updated - - -

I founded this thread in this forum:

https://www.learncpp.com/cpp-tutorial/42-global-variables/

I will try this idea. declare how volatile and how extern volatile in the others programs.

Ty a lot
Moacir Jr.

#### keith1200rs

##### Super Moderator
Staff member
Seems to work for me with PIC C30 but the "volatile" seems to be in a different place to you:
Code:
extern unsigned volatile int burst_acc[MAX_PEAK];

Keith

#### horace1

The normal path would be to use "extern", but wont works.

In the compiler phase, its OK, but in liker phase, I get an error that, for sample, the variable "pbyte" was not found in "xyz.o"

Any1 have any idea how I solve this problem ?
it sounds like you have not included at link time the file where "pbyte" is defined, i.e.

you define it in abc.c
Code:
volatile unsigned char pbyte;     // define pbyte

then declare it extern in xyz.c
Code:
extern volatile unsigned char pbyte;    // declare pbyte extern
did you include abc.o at link time?

or have you declared it extern in all the files?

#### moahrs

##### Junior Member level 2
Hi horace1,

or have you declared it extern in all the files?

I have declared in all files... I think my mistake was it.

I see now that I should place "volatile" in main.c and "extern volatile" in abc.c. is it ?

ty a lot
Moacir Jr.

#### horace1

Hi horace1,

I have declared in all files... I think my mistake was it.

I see now that I should place "volatile" in main.c and "extern volatile" in abc.c. is it ?

ty a lot
Moacir Jr.
yes, you have two concepts - definition and declaration

the definition of a variable specifies its type and identifier (e.g. in main.c)
Code:
volatile unsigned char pbyte;     // define pbyte
this (in effect) creates the variable so at link time storage is allocated for it
you can only define an identifier once or at link time you get "multiple definition of ..." errors

to use the identifier in another file you declare it extern (e.g. in abc.c)
Code:
extern volatile unsigned char pbyte;     // define pbyte
which says there is a variable pbyte defined elsewhere in the project and will be linked at link time (unless it is not defined and you get missing identifier at link time).

you have similar concepts with functions in the declaration of function prototypes and definition of the function with a body of code.

Status
Not open for further replies.