Search code examples
carmkeil

Variable was declare with a never-completed type


I have three files

  • main.c
  • myStruct.h
  • myStruct.c

I read some post about where to define a structure and encapsulation and I want to declare my structure in the header file and define it in the source file.

This is what I have tested.

myStruct.h

// myStruct.h
#include "stdint.h"

typedef struct myStruct myStruct_type;

myStruct.c

// myStruct.c
#include "myStruct.h"

struct myStruct {
    uint32_t itemA;
    uint32_t itemB;
    uint32_t *pointerA;
    uint32_t *pointerB;
};

main.c

// main.c
#include "myStruct.h"

myStruct_type testStruct;    // This is where I get the error message

int main (void) {
    while (1);

    return 0;
}

When I try to compile (Keil uVision) I get the following error "Variable 'testStruct' was declare with a never-completed type myStruct_type testStruct"

What have I missed?


Solution

  • You cannot declare testStruct like that, myStruct_type is an incomplete type. You can at best declare a pointer.

    So change

    myStruct_type testStruct;
    

    with

    myStruct_type *testStruct;
    

    You can think of it like, while compiler is compiling main.c, it does not have information on members in myStruct_type, so it cannot calculate the size of the structure.