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
Your b.h is buggy. A header using data types defined in another one has to include that other header, i.e. b.h should read:
#ifndef MY_B_H #define MY_B_H #include "a.h" struct B { struct A ab; }; #endif /* MY_B_H */
"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"
Well, I just tried
#include <stdio.h> #include "a.h" #include "b.h" void main( void ) { printf( "Hello, world!\n" ); }
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"
#include "a.h"
Thanks guys, I think I forgot to save the changes to "a.h". It works now.... "a.h" contains a lot of things including one struct needed for "b.h". I am more inclined to include headers from other development groups in .c rather than .h (person reasons). Anh