cancel
Showing results for 
Search instead for 
Did you mean: 

Embedded C question, sharing structures over different files.

rael
Associate II
Posted on May 31, 2009 at 22:13

Embedded C question, sharing structures over different files.

6 REPLIES 6
rael
Associate II
Posted on May 17, 2011 at 13:13

Hello all, still getting used to using C for the first time on a project instead of assembler , VB etc.

On my main. C program, I have declared a structure:

typedef volatile struc

{

vu32 req;

vu32 actual;

} axis;

I then used this structure in the creation of axis information:

axis w,x,y,z;

In my stm32f10x_it.c file, I want to be able to use these variables to control stepping of my stepper motors,

so I thought I had to declare them ''extern'' like normal variables:

I tried the to declare the individual ''instantiations''? of the structures like this:

extern w,x,y,z;

but when I try to manipulate data within the structure members:

s32 temp;

temp=w.req-w.actual;

, I get an error indicating that the stm32f10x_it.c has no scope to see the variable that I thought I had provided scope to see, an error that the a request has been made for members: req and actual, in something that is not a structure or union.

So, I need to use different syntax to tell the second program to properly extern -alise w,x,y and z so they can be found complete with structure information, in main.c

Can any of you explain what I'm doing wrong please ?

:o

picguy
Associate II
Posted on May 17, 2011 at 13:13

It looks like you need to understand C scope rules.

Specifically, data or a structure defined *within* a subroutine is out of scope to all other code. This can be good as it assures no other code can use those items. Unless that’s not what you want.

st3
Associate II
Posted on May 17, 2011 at 13:13

Quote:

Can any of you explain what I'm doing wrong please ?

It's hard to say what you're actually doing wrong, because you haven't clearly explained what you are doing, nor precisely wha error messages you are getting.

Quote:

I thought I had to declare them ''extern'' like normal variables

Yes, it works the same as for any other variables - but you also have to make sure that the structure type is also ''in-scope''

You sould have something like this:

header.h

Code:

typedef volatile struct

{

vu32 req;

vu32 actual;

} axis;

// These are just DECLARATIONS of the names & types;

// ie, no memory is reserved here.

// Note: Don't put them all in one declaration - there are traps for the unwary...

extern axis w;

extern axis x;

extern axis y;

extern axis z;

main.c

Code:

include ''header.h''

// These are the actual DEFINITIONS of the variables;

// ie, these are the lines that actually reserve memory space

axis w;

axis x;

axis y;

axis z;

other.c

Code:

include ''header.h''

// This file (and any others) accesses the variables via the

// extern DECLARATIONS provided in the header

Every symbol in your program must be defined exactly once (not necessarily in the same file as main), but may be declared as many times as you like.

Remember, the declarations simply provide information to the compiler; it's only the definitions that actually create ''objects'' in memory...

See:

http://c-faq.com/

rael
Associate II
Posted on May 17, 2011 at 13:13

Thank you ST7,

I will try that way.

Is there a functional difference between the *.c files and the *.h files at the compiler level though ?

st3
Associate II
Posted on May 17, 2011 at 13:13

Quote:

Is there a functional difference between the *.c files and the *.h files at the compiler level though ?

None at all - it is merely a matter of convention that files to be #included are given the .h suffix (and called ''header'' files), and the enclosing source files are given the .c suffix.

The compiler (and Linker) cares only about rules like ''exactly one definition'' - it matters not whether you achieve that in the ''conventional'' way - with headers, etc - or by other means...

Remember that #include, etc, is handled by the preprocessor; the compiler sees only the result of the pre-processing - not the original .h and .c files.

This is all standard 'C' stuff - nothing specifically to do with embedded - so, for full details, study any good 'C' textbook.

rael
Associate II
Posted on May 17, 2011 at 13:13

Thanks again for your help ST7,

A fresh day, and I'm trying those changes.

:D