Header Files / Including
Hey everyone, I'm hoping you can help me with a simple question I've never been able to find a clear answer for.
Basically, let's just assume I'm making a program and have the following files:
1. Does this make sense?
a.h - defines/prototypes/typedefs for code in a.c
a.c - code
b.h - defines/prototypes/typedefs for b.c
b.c - code
common.h - defines and typedefs used in all code files
main.c - main() / other code
2. Assuming I have include guards and am compiling this into a single executable, where should my includes be? For example, I feel that I need to "#include a.h" in a.c, but what about "#include a.h" in main.c if I'm calling the functions there? And where should I include common.h?
You only need to include header files in files which need a definition from the header file. So if main.c is calling functions in a.c, and the prototypes for those functions are in a.h, then you should include a.h in main.c.
You should include common.h in every file that depends on something defined within it. You can use a trick if you want to avoid having things defined multiple times by the preprocessor. It would be something like this:
/* Contents of common.h */
/* put a bunch of definitions here...*/
Thanks a lot, I'll be sure to do it like that. Hopefully I can get such basic organization down so I can finally focus on the code.