Thread: Trying to make the best use of header files

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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."

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

  3. #3
    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