views:

291

answers:

5

I have a file named cpu.h that includes two other headers named register.h and addrmode.h. A cpu_t struct is defined in cpu.h that the two includes need for their functions. I try to include cpu.h in the two other include files, but nothing is included. I am guessing they are not included because of the include guards set up in cpu.h. Does anyone know how this could be solved?

+10  A: 

Declare cpu_t in its own header file that the other three include, perhaps types.h?

Oren Trutner
Thanks a ton man, although I should have thought of that :)
Cory Walker
To add to that, put an include guard in cpu_t's header file, and #include it everywhere you use that struct, that way you don't suddenly lose an internal dependency if you ever remove one of those other header files.
David Claridge
A: 

You could define struct cpu_t in cpu.h before it includes the other two headers?

Actually, I like Oren Trutner's answer better. My answer is much more fragile --- you really don't want mutually recursive header files.

Dave Hinton
I agree too, seems like a hack to me
Cory Walker
A: 

You have stated that "nothing is included", but that is a deduction based upon some other observation. What error message are you seeing? I think we need more information.

digijock
No error message, just the fact that none of the definitions can be accessed from the include files. This is because of the include guards, and gcc is really not including cpu.h.
Cory Walker
+3  A: 

Circular includes can become a nuisance for code maintenance and debugging. I would suggest splitting cpu.h into two files: one that register.h and addrmode.h include, and one that includes those two files.

Neil
+1  A: 

Either arrange register.h and addrmode.h so that they don't need the definition of the struct or move the declaration of the struct to its own header.

Note that you don't need the definition of cpu:

  • to define a typedef for struct cpu:

    typedef struct cpu cpu_type;

  • to define a variable or a member of type pointer to struct cpu:

    struct cpu *ptr;

  • to declare an extern variable of type struct cpu:

    extern struct cpy myCpu;

  • to declare function taking a struct cpu argument:

    void foo(struct cpu p);

So the major reasons to need the definition of a structure in an header whose aim isn't to provide that definitin is:

  • you define in the header a structure having a member of type struct cpu:

    struct intelcpu { struct cpu base; };

  • you define inline functions needing to access members or having local variable of that type

AProgrammer
Don't you have to be careful if you need register.h and addrmode.h in the file where the functions that use the internals of the structure. If you include cpu.h first, then 'struct cpu;' clobbers the previous definition and there are no members in the now incomplete structure; and if you don't include cpu.h first (of all headers except perhaps config.h) then you haven't shown that cpu.h can stand on its own, which impacts its usability (and contravenes the sensible C coding standard promulgated by NASA's Goddard Space Flight Center).
Jonathan Leffler
Where did you get that having struct cpu; would clobber a previous definition? How would you define mutually recursive types if a declaration without definition wasn't possible. In fact, I was a little too influenced by my C++ practice, a line reading struct cpu; alone is not needed in C (in C++ it has the same effect as typedef struct cpu cpu; as tag names are automatically type names). I'll complete my answer.
AProgrammer