Thread: Should I include libraries in header file or in the source file

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    96

    Should I include libraries in header file or in the source file

    I was wondering if I should include the libraries I need in a header file which I create and then just include that file in my program or should I include all of them individually in my source file and leave them out of the header. Or does it even matter which I do?

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    It is recommended and good practice to create a header file that hosts all your includes, and in this file, an include guard. In all your subsequent source files, only that header file will be included. Here's an example:

    includes.h
    Code:
    #ifndef __INCLUDES_H_
    #define __INCLUDES_H_
    
    #include <windows.h>
    #include <stdio.h>
    #include "prototypes.h"
    
    #endif
    main.c
    Code:
    #include "includes.h"
    
    int main(void)
    {
         return someFunction();
    }
    prototypes.h
    Code:
    int someFunction(void);
    someFunction.c
    Code:
    #include "includes.h"
    int someFunction(void)
    {
         return 0xdeadbeef;
    }
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, that is not always a good idea. For one thing, if you are including, say, "windows.h" in in every source file, then that is a BIG waste of time if you are not calling windows functions in every (or nearly every) source file. And a good idea is to keep system-dependant functions (e.g. Windows API functions) to one or a few files, and write most of the code in a system-independent way.

    It is also a very good way to understnad what the dependencies of a particular file is, by reading the list of header files that it includes. You can not tell if "x.c" uses "y.c" functions if "y.h" is in "includes.h" that is included by EVERYTHING. Since such dependencies are quite useful to know [if you for example want to change the declaration of a function in y.h, you may want to know who uses y.h], including the individual header files in each file that needs them is a better option.

    It is fine to let a.h include b.h, so that you don't have include dependencies within a source file (so x.c doesn't HAVE to include b.h before a.h, simply because a.h needs b.h).


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

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That approach usually works with precompiled header files. However, I cannot say it's good practice.
    Good practice is that every header includes every other header that it needs to compile successfully.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Unable to open include file SDL_audio.h
    By rraj.be in forum C Programming
    Replies: 2
    Last Post: 06-28-2008, 08:04 PM
  3. C programing doubt
    By sivasankari in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 09:19 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM