Thread: Headers circular referencing problem

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    Exclamation Headers circular referencing problem

    Hi,
    The problem is:

    Header A-> include B
    Header B-> include C
    Header B-> include A
    Header C-> include A

    All the header have a pointer to struct that reside in include header.
    How can i resolve this problem with preprocessor directivies?

    Thanx

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    1. redesign them. put everything into one header file.

    2. use preprocessor directives to insure the header is included only once.
    Code:
    #if defined(_HEADER_A_H)
    #define _HEADER_A_H
    // forward declarations here
    struct xyz; // this is in another header file
    // rest of header file here
    // blabla
    
    #endif

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Ancient Dragon, close, but not quite:
    Code:
    #if !defined(_HEADER_A_H)
    #define _HEADER_A_H
    // forward declarations here
    struct xyz; // this is in another header file
    // rest of header file here
    // blabla
    
    #endif
    Remember, you want to compile the following code if it has not been compiled.

    Look at the standard header files for examples. I usually use ifndef:

    Code:
    #ifndef FILENAME_H
    #define FILENAME_H 1
    
    /* stuff in header file that can only be included once */
    
    #endif
    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.

  4. #4
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    I usually use ifndef:
    Me too. For me it's a nice little mnemonic because ifdef and define have the same number of characters. In my editor if the two lines for inclusion guards don't line up, that sets off a warning flag in my mind and I usually need to change ifdef to ifndef to fix it.

    Just on a side note since no one has mentioned it yet, identifiers leading underscores followed by an upper case letter are always reserved by the implementation. So it's possible that mysterious breakage could happen with Ancient Dragon's example.
    Just because I don't care doesn't mean I don't understand.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    if you use !defined(...) you can have more than one expresssion
    Code:
    #if defined(_MSVC) || defined(_BORLAND) || defined(_GCC)
    // blabla
    #elif defined(_UNIX) && !defined(_X11)
    // blabla
    #elif defined(_UNIX) && !defined(_MAC)
    // blabla
    #endif
    You can't do something like above with #ifdef statements


    > identifiers leading underscores followed by an upper case letter are always reserved by the implementation

    I often ignore rules like that. That's just a guideline, not a rule that is enforced by any compiler.
    Last edited by Ancient Dragon; 09-10-2005 at 03:13 PM.

  6. #6
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    if you use !defined(...) you can have more than one expresssion
    Which is irrelevant with inclusion guards because you only need one unique identifier per header. In general I agree that defined() is more flexible, but for inclusion guards I don't see the need.
    You can't do something like above with #ifdef statements
    But that wasn't a multiple inclusion guard, which is what I was talking about since that's what the topic of the thread is.
    That's just a guideline, not a rule that is enforced by any compiler.
    It doesn't have to be enforced. It'll just break silently and with strange behavior. You'll be searching for days to find a bug like that. Ignore it if you'd like, but I've been bitten by that particular bug before, and it's not pretty.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  2. Class Include Problem (I Think)
    By gpr1me in forum C++ Programming
    Replies: 8
    Last Post: 03-21-2006, 12:47 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM