Referencing Glogal Structures  CW for Coldfire 6.4

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Referencing Glogal Structures  CW for Coldfire 6.4

1,339件の閲覧回数
BillH
Contributor I
In a file Globals.c I have a structure defined.
 
static struct
{
 int16 nHarmonics;
 int16 HIndex[16];
 uint16 HAmplitude[16];
 uint16 HPhase[16]; 
}g_stWaveforms[6];
When I try to access the structure in other .c files using
 
external struct g_stWaveforms[];
 
Error   : illegal use of incomplete struct/union/class 'struct g_stWaveforms'
Command_Processor.c line 17   extern struct g_stWaveforms[]; 
 
Error   : undefined identifier 'g_stWaveforms'
Command_Processor.c line 72      g_stWaveforms[t_tmp_ui16a].nHarmonics =Get16( &g_CommandBuffer[t_cmd_offset+3);
 
How can I resolve this?
 
Thanks
ラベル(1)
タグ(1)
0 件の賞賛
2 返答(返信)

312件の閲覧回数
CrasyCat
Specialist III
Hello
 
Well there are several problems in this code snippet.
  1- If the variable is defined a static in the file globals.c, it will not be visible outside of that module.
     So if you want to use that variable in another module remove the static modifier in front of the
     variable definition in globals.c
  2- If the variable has a structured type, this type should be known from all module using that variable.
 
I would suggest you the following:
  1- Define a file globals.h, where you define a typedef for the structured type. It is also good practice
       (or programming style) to insert the external declaration for the variable in this module.
      For example:
          typedef  struct 
          {
             int16 nHarmonics;
             int16 HIndex[16];
             uint16 HAmplitude[16];
             uint16 HPhase[16]; 
          }t_stWaveforms;
 
          extern t_stWaveforms g_stWaveforms[];

   2- Include globals.h in globals.c and define the variable there.
      For Example
         #include "globals.h"
          t_stWaveforms g_stWaveforms[6];
   3- Include globals.h in each C source file where you want to use the variable g_stWaveforms.
 
Note that this is all standard ANSI C modular programming rules. You may also refer to your favorite ANSI C reference manual for more information around that subject.
 
I hope that helps.
CrasyCat
0 件の賞賛

312件の閲覧回数
BillH
Contributor I
Thanks,

It's be a decade or more since I did any C programming and I've got to re-awaken all of the old dead brain cells.  I got everything working. 

Thanks again.

Bill
0 件の賞賛