Thread: General Guidelines on Preventing Header File collision

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    General Guidelines on Preventing Header File collision

    I have tried to search on Google as well as this forum's (and others) about general guidelines in preventing header file collisions when including them in a .c or .cpp file, but I am having very little luck finding it properly. Does anyone have tips in general in preventing this from happening in general (i.e. how to find out what order the header files need to be included)? I think this question may have been asked in other phrases than the ones I've searched, but I'm not sure what terms would be more suitable to perform a search. Thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Are you talking about inclusion guards? (Search for that and you'll probably find a lot of information).

    Basically, you use
    Code:
    #ifndef SOMETHING
    #define SOMETHING
    
    /* contents of header file . . . . */
    
    #endif
    That prevents a header file from being included more than once.

    [edit] There was a thread about this earlier today: http://cboard.cprogramming.com/showthread.php?t=104721 [/edit]
    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
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Actually, in some cases you need to include the headers in a specific order.

    For instance, if you include <windows.h>, you need to include it before the other headerfiles, because in rare cases there were some kind of headerfile that overrided something from <windows.h>
    Or so I've read.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Partially, I'm talking about what dwk described about inclusion guards; partially, I'm also talking about what Drogin described. I was having trouble finding out about the order of header file inclusion in the documentation (I just want to know if there's a standard/systematic way rather than experimenting with the order to get one that works).

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I guess a good rule of thumb would be to include the most general header first. Including a specific header file first would likely draw in the generic header files, so putting the generic header files after specific ones would just be useless.
    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.

  6. #6
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Partially, I'm talking about what dwk described about inclusion guards; partially, I'm also talking about what Drogin described. I was having trouble finding out about the order of header file inclusion in the documentation (I just want to know if there's a standard/systematic way rather than experimenting with the order to get one that works).
    Besides putting <windows.h> on top, I don't know.
    I found out why.

    Both the WINNT.H and WCHAR.H header files are protected against redefiniton of the TCHAR data type if they've been defined by thease headers. However, if you're using other header files, you should include <windows.h> first.
    (From Programming Windows Fifth Edition, by Charles Petzold)

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Might be a coincidence, but I think that windows.h is more generic than winnt.h or wchar.h . . . .
    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.

  8. #8
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    <windows.h> includes WINDEF.H, WINNT.H, WINBASE.H, WINUSER.H, WINGDI.H

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oh well, what do I know.

    Anyway, there's no real way to tell which header should be included before other headers. It depends on how they're created, and you can create header files however you like.
    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.

  10. #10
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    I don't really know either...I just read from Petzold's book.
    When it comes to windows, he's like a...god

    You can se, he have even tatooed windows on his arm.
    http://upload.wikimedia.org/wikipedi...es_petzold.png

  11. #11
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    For headers, inclusion guards solve everything. I usually use the filename, replacing . with _. For example, fileio.h would be:
    Code:
    #ifndef FILEIO_H
    #define FILEIO_H
    
    // Stuff
    #endif
    If a.h requires that b.h be included before it, then a.h includes b.h itself. At the .cpp level, I can then #include in any order. (Except in some really exotic situations that were a result of me not thinking far enough into the future. Plan, plan, plan.) Now, if it's someone else's headers, then that's another story. I generally do:
    Code:
    // Standard C/C++ headers
    // Headers for libraries, ie, SDL, curl, etc.
    // Headers for this project.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Drogin View Post
    When it comes to windows, he's like a...god
    By that, I take it you mean many cite signs of his greatness but many others wonder if he actually exists.

    Seriously, the general guideline is that header files should provide everything needed for them to be used: if there is no way for code to effectively make use of header A without also #include'ing header B (eg if header A makes use of definitions or declarations in header B) then header A should generally #include header B.

    As an almost general rule, all headers should have #include guards as well. This is virtually mandatory if there is any chance of a header file being #include'd more than once (eg nothing prevents code from #including a header twice, and it is possible for some code to #include two headers that each include some other header). There are exceptions to this rule (eg a header file that works differently based on optional #define's which can be redefined between two inclusions), but anyone running into such exceptions will know exactly what they are doing.

    The unfortunate thing is that some header files with some commercially available libraries fail to comply with such guidelines ..... so it is necessary for users of those libraries to work around such cases.
    Last edited by grumpy; 07-05-2008 at 07:54 PM. Reason: Fixed typos

  13. #13
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    By that, I take it you mean many cite signs of his greatness but many others wonder if he actually exists.
    Aye. The unix heritics doesent belive.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Replies: 6
    Last Post: 04-02-2002, 05:46 AM