Page 1 of 1

using header files

Posted: Fri Feb 27, 2009 11:55 am
by yemista
I was just wonder if its considered bad practice to have one header file for many source files. For example, Im working on my memory manager, and I decided I want top break it down into peices. One piece that handles virtual addresses, and another that handles physical. However, both of these components are part of the same memory manager, and I was considering putting all the function declarations in mm.h, even though all the functions wont be in the same source file. Im just trying to avoid having such a growing number of header files, when if Im working with the memory manager, I should be able to include mm.h, instead of paging.h, frame_allocator.h, etc etc.

Re: using header files

Posted: Fri Feb 27, 2009 12:03 pm
by Troy Martin
It's best to have one header file per source, but if you use ifndef, define, endif statements around your headers, you could include all the paging.h, stack.h, etc. in a header file called mm.h and then use that.

Re: using header files

Posted: Fri Feb 27, 2009 8:03 pm
by neon
For example, Im working on my memory manager, and I decided I want top break it down into peices. One piece that handles virtual addresses, and another that handles physical. However, both of these components are part of the same memory manager
No they are not -- not if you break it down to smaller components. While the components might work together for a bigger interface, they are both very different interfaces and abstractions. i.e., you have a physical memory manager and a virtual memory manager on top of that. Collectively this can be coinsidered a memory manager; but it is two managers not one.
and I was considering putting all the function declarations in mm.h, even though all the functions wont be in the same source file.
If a component contains multiple source files, it is fine to put all of the function declarations in the same header file. However, keep in mind that header files should only expose the interface of the component. If they do not, then the header file should be part of the implementation, which is where the one header per source file rule comes in. If it is part of the implementation, but still as a standard "master" include file, that is bad design (common.h-like header files should be avoided.)