Thread: Trying to make the best use of header files

  1. #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?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    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."

  4. #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).

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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