Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
struct in Background C program
#1
My single file background c program was getting unwieldy so I decided to split it into 1 header and 3 c files. Now I get some strange errors saying my struct is not defined. So I copied my struct to the capp sample program header file and it also didn’t work. Then I defined my struct within the capp.c file and it did compile. What am I missing?
__________
/*For more information see notes.txt in the Documentation folder */
#include <gplib.h>

#define _PPScriptMode_· · // for enum mode, replace this with #define _EnumMode_
#include "../../Include/pp_proj.h"

//#include "capp1.h"

typedef unsigned long DWORD;

typedef struct _SStatus
{
· union {
· · struct {
· · · DWORD abUnusable : 8;
· · · DWORD bMotionBusy : 1; //0b23
· · · DWORD bInitializeEnd : 1; //0b22
· · · DWORD bAllServoOn : 1; //0b21
· · · DWORD bAlarm : 1; //0b20
· · · DWORD bHomePosition : 1; //0b19
· · · DWORD bStandbyPosition : 1; //0b18
· · · DWORD bExchangePosition : 1; //0b17
· · · DWORD bSystemReady : 1; //0b16
· · · DWORD abUnused :16;
· · };
· · DWORD dwStatus;
· };
} SStatus;

int main(void)
{
· InitLibrary(); // Required for accessing Power PMAC library

· · · · · //Put your code here
· SStatus status;
· status.bMotionBusy = true;

· CloseLibrary();
· return 0;
}
Reply
#2
You can move your structure definition to a .h file. Here is my example which sets P0=12 through the member of a structure defined in structs.h.

capp1.c
Code:
/*For more information see notes.txt in the Documentation folder */
#include <gplib.h>  

#define _PPScriptMode_        // for enum mode, replace this with #define _EnumMode_
#include "../../Include/pp_proj.h"
#include "structs.h"

int main(void)
{
    InitLibrary();  // Required for accessing Power PMAC library

    struct AStruct MyStruct;
    MyStruct.MyVar1=12;
    pshm->P[0]=MyStruct.MyVar1;

    CloseLibrary();
    return 0;
}

structs.h
Code:
typedef struct AStruct
{
    double MyVar1;
    double MyVar2;
};
Reply
#3
Thanks for the reply, Eric.

So if in the same c file I can use the format
typedef struct _SStatus
{
}SStatus;

but if in a header file this doesn't work but
typedef struct SStatus
{
};

does work. What about my typedef unsigned long DWORD;? That also works fine when everything is in a .c file. How to make that work in the .h file?

I attached a simple sample of my issues. The forum said the .h file type wasn't allowed so I renamed to .txt. I put the same code in one file and in the header file. It will compile as is but if you comment out the 2 typedefs in the .c file and rely on the definitions in the header it won't compile.

If there's a sample of some more complicated program I could glean the compile rules from that would be ideal. Or maybe I'm doing something fundamentally wrong -- it seems strange to me that the same code should be handled differently in header and c file.

Thanks!


Attached Files
.c   capp1.c (Size: 944 bytes / Downloads: 3)
.txt   capp1.txt (Size: 511 bytes / Downloads: 3)
Reply
#4
In case anybody else runs into this, if the struct is defined in a header file, its format should be typedef struct Name{definition}Name;
Both Names must be the same and both must be present.

If the struct is in the .c file it can be declared more flexibly.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)