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
  • 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 */

    (The #ifndef/#define stuff is standard practice to make headers safe for multiple inclusion.)

    As to the compiler error you got: that shouldn't happen 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.

Reply
  • 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 */

    (The #ifndef/#define stuff is standard practice to make headers safe for multiple inclusion.)

    As to the compiler error you got: that shouldn't happen 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.

Children
  • "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.

  • Well, I just tried

    #include <stdio.h>
    
    #include "a.h"
    #include "b.h"
    
    void main( void )
    {
    	printf( "Hello, world!\n" );
    }
    and it compiled without error!

  • 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.

  • 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