C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-31-2009, 04:19 PM   #1
Registered User
 
Join Date: Aug 2009
Posts: 24
Trying to make the best use of header files

Until now I have been putting all function declarations in a single global header file (except for those that have an obviously limited need of scope).

I am now considering putting all declarations in the header file corresponding to the source file where the definition is found, and include that header in all source files that need to call one of those functions.

I'll still use the global header for the declarations that are so widely used that I can't seem to keep track of who wants them...or I could include such individual headers in the global header file rather than pull the individual declarations across.

Does this sound like a plan?
alanb is offline   Reply With Quote
Old 08-31-2009, 04:22 PM   #2
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Sure. Until you get into more complicated stuff, having a corresponding .h file for every .cpp file is a great idea. You just put the function prototypes and so on that are defined in the .cpp file in the appropriate header file.

You'll probably also end up wanting to create, for example, somesubsystem.h, which includes a bunch of header files that you commonly use together . . . but be careful not to overuse this sort of thing. At the least, it means you'll have to wait a long time for recompilation if you modify a commonly-used header. And it might make the code harder to modify in the future . . . .
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks is offline   Reply With Quote
Old 08-31-2009, 04:22 PM   #3
Ex scientia vera
 
Join Date: Sep 2007
Posts: 464
Generally, trying to include only the header files that contain declarations/definitions for what you actually need in your code is best practice, I believe. This also reduces compile time when you get larger projects, as long as you are using a makefile or some such. It will only compile the files that have been modified.

Knowing a bit more about your program and how you are structuring it might help people with giving you some advice.
__________________
"What's up, Doc?"
"'Up' is a relative concept. It has no intrinsic value."
IceDane is offline   Reply With Quote
Old 08-31-2009, 04:43 PM   #4
Registered User
 
Join Date: Aug 2009
Posts: 24
Quote:
Originally Posted by dwks View Post
somesubsystem.h,
OK, so I gather that global header files should be used judiciously and preferrably only semi-globally.

Quote:
Originally Posted by IceDane View Post
Knowing a bit more about your program and how you are structuring it might help people with giving you some advice.
Say, I want to build a world. I have one file for starting and getting a window, one for initialising the graphics system, one for rendering - which has everything from model definitions to projection (may eventually break this into two), and one for movement which has everything from keyboard input to physics calculations (may also break this one up in the future).
alanb is offline   Reply With Quote
Old 08-31-2009, 04:45 PM   #5
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Quote:
Originally Posted by alanb View Post
OK, so I gather that global header files should be used judiciously and preferrably only semi-globally.
Definitely.

Quote:
Say, I want to build a world. I have one file for starting and getting a window, one for initialising the graphics system, one for rendering - which has everything from model definitions to projection (may eventually break this into two), and one for movement which has everything from keyboard input to physics calculations (may also break this one up in the future).
That sounds like a reasonable breakup. If you can describe what the file does in one word, such as input.cpp, then you're probably okay.

Once you start getting a lot of code, you might even want to break up your code into different subdirectories; graphics/, gui/, network/, etc. Then you can put code in each directory into a namespace of the same name. But you shouldn't have to worry about that for a while.
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Source Files, make files, scope, include thetinman C++ Programming 13 11-05-2008 11:37 PM
Help using Header Files d34n C Programming 8 04-21-2008 11:06 PM
added start menu crashes game avgprogamerjoe Game Programming 6 08-29-2007 01:30 PM
What /what not to put in header files Just C Programming 1 12-14-2002 10:45 AM
Header files Jez_Master C++ Programming 2 04-08-2002 07:56 AM


All times are GMT -6. The time now is 02:13 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22