r/C_Programming • u/Suitable_Broccoli361 • Jun 27 '26
Question I need help on the header files declarations in my project
Hello!
I have alot of header files in my project, there are 3 header files that are causing an error in a specific state and I'll simplify their name like this:
Globals.h
Header1.h
Header2.h
I have a struct in Header1.h and I also have a struct in Header2.h
I want both structs that are declared in Header1 and Header2 to be visible to Globals.h, so I included these header files in Globals.h
But at the same time, I need to include Globals.h inside header1.h and header2.h which is causing alot of errors
So it's like this:
Global.h includes header1.h and header2.h
Header1.h and header2.h includes Globals.h
What can I do about this? Sorry for my bad explanation
9
u/flyingron Jun 27 '26
You need to figure out what is causing the circular dependency and work around.
If all you need in the other files is pointers or references to the structs involved, you can move their actual definition elsewhere and just put a incomplete declaration (forward reference).
For example, if you just need to know tha header1struct, you can do in globals.h (possibly adding a different include file):
struct header1struct;
struct globalStruct {
struct header1struct* h1;
};
Otherwise, you're going to likely have to split up the things that you really need to include into header1.h etc.. into different files and not declare the whole globals.h etc...
15
u/TrondEndrestol Jun 27 '26
I hope you use a defensive mechanism for repeated includes in each header file.
1
u/SmokeMuch7356 Jun 28 '26
Circular dependencies are bad and you need to rethink your design. If you're creating instances (variables) of those struct types in Globals.h then you're out of luck; you'll have to repartition your code to break those circular dependencies.
You should avoid using global variables where possible; if you need something that persists over the lifetime of the program, declare it in
mainand pass it as an argument to anything that needs it.Headers should contain macros, type definitions, constant definitions, and function declarations. You should not define functions or create variable instances in header files.
1
u/duane11583 Jun 27 '26
so do you need the contents of the structs or only a pointer to the structure?.
often you only need a pointer to the struct.
if so add this to the top of the files:
struct foo;
struct bar;
list all of them
what you are creating is a vacuous definition.
this is sort of how a link list is created that points to itself
ie
struct node;
struct node {
int data;
struct node *ptr_next;
};
.
1
u/ReallyEvilRob Jun 28 '26
I think you're making some needles complexity. Don't define globals in your headers. Global variables are part of your implementation and belong in your .c files. Header files should only contain the interface for your module. Things like function prototypes, forward structure declarations, and external variables are the interface to your module.