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).
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.
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
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.
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
'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.