Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

volatile variable as global (extern volatile?)

Status
Not open for further replies.

moahrs

Junior Member level 2
Joined
Dec 29, 2011
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,459
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.edaboard.com/threads/242177/

And this link in thread:
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
Joined
Oct 9, 2009
Messages
10,877
Helped
2,065
Reputation
4,130
Reaction score
1,597
Trophy points
1,403
Location
Yorkshire, UK
Activity points
57,269
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

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,069
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
Joined
Dec 29, 2011
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,459
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

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,069
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top