This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

help #include

a.h:
struct A {
   int a;
};

b.h:
struct B {
   struct A ba;
};

c.c:
#include "a.h"
#include "b.h"

</prev>

compiler complains struct A in file b.h is undefined.  I would think that by include a.h before b.h in c.c, it would see the struct A definition?

I know I could make it compile by adding:
struct A; in b.h but just want to know why it doesn't work the way above.

Thanks,

Anh

Parents
  • But in the example he posted, Anh had included both the headers:

    ... in the .c file. That's not solving the problem, it's just avoiding the symptoms. File b.h, as-is, is incomplete. You can't just put

    #include "b.h"
    in any given C source file --- you have to remember to
    #include "a.h"
    first. That's a deficiency of b.h. It may not seem too bad in this simple case, but it'll bite back as soon as you try that same tactics in larger projects.

Reply
  • But in the example he posted, Anh had included both the headers:

    ... in the .c file. That's not solving the problem, it's just avoiding the symptoms. File b.h, as-is, is incomplete. You can't just put

    #include "b.h"
    in any given C source file --- you have to remember to
    #include "a.h"
    first. That's a deficiency of b.h. It may not seem too bad in this simple case, but it'll bite back as soon as you try that same tactics in larger projects.

Children