EmbDev.net

Forum: ARM programming with GCC/GNU tools help with c code again


von Sevc D. (sevc)


Rate this post
useful
not useful
Hi all.
I have trouble to define structure for all source file.
Before Igive question to how define structure to flash but I define
to ram and define structure *.h files.

typedef struct {
    float  Vmax;
    float  Amax;
    float  Step;
    short  Hold_power;hold ax
    short  Move_power;
                short  Hold_les_power;
    short  Move_more_power;
    } SettingsAX ;

then define :  volatile SettingsAx setting_x =
{300,100,96,20,80,20,120};

but I wat see this structure from all source file to acces it.
compiler show me error : multiple definition . How to correct this
mistake???

regards .

PS: I have book C for microkontroler but it's write for IAR compiler and
some example from book can't use (WINARM conmpiler show error).

von Martin T. (mthomas) (Moderator)


Rate this post
useful
not useful
Get a "general" C-programming book/tutorial (there are several available
online for free). Read about declartion and defintion of variables and
the extern keyword.

von Sevc D. (sevc)


Rate this post
useful
not useful
Martin Thomas wrote:
> Get a "general" C-programming book/tutorial (there are several available
> online for free). Read about declartion and defintion of variables and
> the extern keyword.

I use extern keyword but compiler show warning :
axes.c:18: warning: 'Ax_p_x' initialized and declared 'extern'

I declare it on axes.c file, and structure is define on axes.h .
I want access to this structuro from main function.
I define this in axes.c:
extern volatile Ax_parameter Ax_p_x = {0,0,0,0,0,0,0,0,0,0,0,0,0};
In file axes.h define:
typedef struct {
    DWORD Vactual;
    DWORD Vbefore;
    double Vmax;
          double Vneed;
    double Verror;
    double Acceleration;
    double Sactual;
    double Sstep;
    double Sabsolute;
    double Serror;
    double Time;
    DWORD Timer;
    double Terror;
    } Ax_parameter ;

But if access this structure from main then compiler show error :
cnc1.c:68: error: 'Ax_p_x' undeclared (first use in this function)

using this structure in axes.c file is OK

regards

von Sevc D. (sevc)


Rate this post
useful
not useful
If define onnly easy variable like int or char :
extern volatile char c;

then can use on all source file but with structure it donn't work.

von Sevc D. (sevc)


Rate this post
useful
not useful
Now it work.
In header file I define
extern volatile Ax_parameter Ax_p_x;
In source file I define
volatile Ax_parameter Ax_p_x = {0,0,0,0,0,0,0,0,0,0,0,0,0};

and work all.

thx all.

regards

von Clifford S. (clifford)


Rate this post
useful
not useful
'extern' means 'this variable is instantiated somewhere else', it is
only used for 'declarations' not 'definitions'.

When you initialise a variable, you can only do that on a 'definition'.
So you had a definition declared extern, which makes no sense. It will
ignore teh extern keyword and instantiate the variable. But if you do
that in a header you end up an instance every time the header is
included and the compiler or linker will complain if you include it more
than once.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.