-
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:
Code:
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
1. Does this make sense?
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:
Code:
/* Contents of common.h */
#ifndef COMMON_H_
#define COMMON_H_
/* put a bunch of definitions here...*/
#endif
-
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.