C Unknown type name ‘my_structure’

Because of how you’ve ordered your includes, the compiler sees void some_func(my_structure *x); before it sees typedef struct abcd { int a; } my_structure;.

Let’s walk this through.

Assuming my_struct.h is processed first, we get the following sequence of events:

  1. UTILSH is defined
  2. MAINH is defined
  3. Because UTILSH is already defined, we don’t process my_struct.h again, so the typedef isn’t processed
  4. void some_func(my_structure *x); is processed.
  5. Now the typedef is processed.

So after preprocessing, your compiler sees the following sequence of declarations:

...
void some_func(my_structure *x);
...
typedef struct abcd {...} my_structure;

Bad juju. You either need a forward declaration of my_structure in main.h, or you need to break that circular dependency (which is the much preferred option). Is there anything in main.h that my_structure.h actually uses? If so, you will want to factor it out into a separate file that both main.h and my_structure.h include.

Leave a Comment

tech