Thread: Redundant conditional inclusion macros?

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    7

    Redundant conditional inclusion macros?

    Hi guys, I'm looking into the MINIX 3 source code and I often see things like

    Code:
    #ifndef _ANSI_H
    #include <ansi.h>
    #endif
    even though the file ansi.h was "guarded" with

    Code:
    #ifndef _ANSI_H
    #define _ANSI_H
    
    /* ansi.h contents */
    
    #endif /* _ANSI_H */
    Isn't the effect of the first snippet equal to just?

    Code:
    #include <ansi.h>
    Is that to avoid making the compiler open the file to see if it was already included, to protect
    against not "guarded" files, or just bad style? I'm just curious.

    Thanks

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    An include preprocessor command is basically a copy paste of the file you are including. The guards as you noticed are a way to prevent a file to include the same thing multiple times and create conflicts. If you included file1.h in file2.h and then included both file1.h and file2.h in file3.h without the guards you would get an error because stuff declared in file1.h is declared multiple times, once in file1.h and once in file2.h.

    As for what you pointed out, yes, the Minix instructions seem a bit redundant. My guess is that there were multiple developers of that library with different styles.
    Last edited by claudiu; 03-19-2010 at 12:59 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Aculaniveus
    Is that to avoid making the compiler open the file to see if it was already included, to protect
    against not "guarded" files, or just bad style?
    The former is most likely the intent, but it is not very useful on modern compilers that can avoid including the file's contents as soon as the guard is detected at the top of the file.

    You cannot protect against "not guarded" files in this way.

    It is bad style because it requires the file that is included and all the files that include it to agree on the inclusion guard identifier.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    7
    Thanks for the replies guys.

    You cannot protect against "not guarded" files in this way.
    I see, after all after

    Code:
    #ifndef _ANSI_H
    #include <ansi.h>
    #endif
    _ANSI_H is still undefined if ansi.h doesn't define it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  2. redundant allocation
    By George2 in forum C++ Programming
    Replies: 22
    Last Post: 03-06-2008, 06:43 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  5. Parsing and Fwrite Help Please.
    By Freestyler in forum C Programming
    Replies: 11
    Last Post: 12-06-2005, 11:38 AM