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
  • "A header using data types defined in another one has to include that other header"

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

    c.c:
    #include "a.h"
    #include "b.h"
    So, although what you say may be good practice in general, the code as posted should've been OK?

    "unless you tried to compile b.h on its own --- e.g. by adding it to your uVision project as a source file to be compiled -- which you really shouldn't do."

    Quite so.

Reply
  • "A header using data types defined in another one has to include that other header"

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

    c.c:
    #include "a.h"
    #include "b.h"
    So, although what you say may be good practice in general, the code as posted should've been OK?

    "unless you tried to compile b.h on its own --- e.g. by adding it to your uVision project as a source file to be compiled -- which you really shouldn't do."

    Quite so.

Children