using header files

Programming, for all ages and all languages.
Post Reply
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

using header files

Post 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.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: using header files

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: using header files

Post 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.)
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Post Reply