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?
Declare cpu_t
in its own header file that the other three include, perhaps types.h
?
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.
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.
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.
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