Thread: where to include header files?

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    5

    where to include header files?

    Where should I include other header files, in header file or source file? What is the difference between the below two:

    Code:
    //file1.h
    #include "file2.h"
    ...


    OR



    Code:
    //file1.h
    class class2; //class class2 is in file2.h
    ...
    Code:
    //file1.cpp
    #include "file2.h"
    ...
    Both seem to work bu which version is better? Or at least preferred? Does the first version (may) have bad consequences?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As long as the header does not use the type other than reference it through pointer or reference, you can get away with the second, and probably better so.
    For every header you include, the longer the compilation time. Therefore, it is better to add forward declarations as you do, when and if you can.
    The real header must be included in the source file, naturally.

    Just make sure to only include headers you need in each file. No adding stuff just because.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    It's clear now, thanks a lot!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    As long as the header does not use the type other than reference it through pointer or reference, you can get away with the second, and probably better so.
    For every header you include, the longer the compilation time. Therefore, it is better to add forward declarations as you do, when and if you can.
    The real header must be included in the source file, naturally.

    Just make sure to only include headers you need in each file. No adding stuff just because.
    The counter-point to this is that this involves "remembering" which header files require which other header-files to be included. If each header file includes everything that IT NEEDS in itself, then you do not need to remember "Ah, I included file1.h, so I must also include file2.h" - after all, each header-file MUST be included at some point [assuming something from it is actually being used of course]. Nearly all modern compilers [such as gcc and MS Visual Studio] have code in them to recognize duplicated includes [when either include guards are used] that won't need to be included a second (or third, fourth etc) time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    In large projects it's probably better to keep and update a diagram of header/source files with their relationships even if the developers(s) can remember everything, because other people who read the code might get confused. Ideally a project should already have that kind of a diagram before even starting to code IMHO.
    Thank you all

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I have never seen a project that has such a diagram, and I see no need for it. Header dependencies are simple: if some code requires a header, the file includes it. Period. Never assume that another header includes it for you, because this may change.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by kaos_frack View Post
    In large projects it's probably better to keep and update a diagram of header/source files with their relationships even if the developers(s) can remember everything, because other people who read the code might get confused. Ideally a project should already have that kind of a diagram before even starting to code IMHO.
    Thank you all
    This presumes that certain headers and implementation files somehow "belong" together. The purpose of a header file is to define an API.

    The way to reduce confusion about headers is to:

    1) Make your libraries/APIs as cohesive as possible

    2) Only require a single header to access a given library/API, even if this is implemented "under the hood" as multiple headers

    3) Make the name of the header correspond to the name of the library/API so people don't have to guess what file to include

    It is tempting to create many smaller header files in an effort to reduce some imaginary compilation cost. That's definitely premature optimization.

    And to address the original question more specifically, if you need some declaration X, and it's declared in X.h, then you should include X.h. Even if you've already included some other header which you know includes X.h.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Many Header Files
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 03-08-2005, 02:45 PM
  2. Header files
    By GaPe in forum C Programming
    Replies: 6
    Last Post: 02-01-2003, 03:39 PM
  3. Header files
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2002, 07:56 AM