Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Why not multiple inclusion of a header file if certain precautions are taken?

  1. Mar 7, 2008 #1

    Eus

    User Avatar

    Hi Ho!

    I often heard people say that we should avoid multiple inclusion of a header file.
    And, they very often show how to do it.
    But, I myself haven't yet found the reason of not to do it when certain precautions are taken.

    What I am going to discuss here is C programming, not C++.

    I included one single header file containing:
    - function prototype
    - struct declaration
    - enum declaration
    - typedef
    multiple times in several source code files, but my gcc happily compiled the source code files.

    Of course, if I added one variable definition within the header file (e.g. int x = 0), gcc complained of multiple variable declarations.

    But, I know that a variable should be best defined in a source code file not in a header file.

    Therefore, as far as I don't put any variable definition in a header file, there is no problem with the multiple inclusion of the header file, isn't there?

    Is there any reason to say that multiple inclusion of a header file should be avoided at all?

    What do you think?

    Thank you.

    Best regards,
    Eus
     
  2. jcsd
  3. Mar 7, 2008 #2
    I think, the c preprocessor takes the last definition of a meta variable.
     
  4. Mar 7, 2008 #3

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    The standard C library header files - like stdlib.h - look like this, simplfied a LOT:
    Code (Text):

    #ifndef __STDLIB__
    #define __STDLIB__
    stdlib stuff goes in here
    .........
    ...........
    #endif /* __STDLIB__ */
     
    This prevents the header from causing duplicate symbol errors, etc because if the preproccesor tries to include stdlib.h a second time, 99.5% of it is skipped over.
    You can read your header files and see how this is implemented for your compiler.

    None of the include "defines" like __STDLIB__ are defined anywhere else. Don't you do it either.

    If you need to use a macro directive like _INCLUDE_POSIX_SOURCE to have a proper global effect, it has to appear in your code before any include whatsoever.
     
  5. Mar 7, 2008 #4

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    Also, because of the information above, what do you think you will you accomplish by including STDC header files more than one time? All bets are off on homegrown header files that do not follow the above convention.
     
  6. Mar 7, 2008 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Suppose you have some header file foo.h that defines a structure and prototypes a function that uses the structure. A source file that includes foo.h twice as follows will fail to compile:
    Code (Text):

    #include "foo.h"
    #include "foo.h"
    void foo (struct foo * foo_ptr)
    {
       ...
    }
    This, on the other hand, will compile:
    Code (Text):

    #include "foo.h"
    void foo (struct foo * foo_ptr)
    {
    #include "foo.h"
       ...
    }
    The difference is in scope. It is perfectly valid in C to use the same name in different scopes. The first #include occurs at file scope, while the second occurs in function scope. Note well: While it is valid to do this, it is not very smart (exception: obfuscations such as this help in winning the IOCCC).

    The c preprocessor is not that "smart". All that #include "foo.h" means is to replace the #include line with the body of foo.h. If the code says to do it twice, the preprocessor will do it twice. Hmm. I wonder if anyone has written a IOCCC entry that uses multiple includes in a clever way?
     
  7. Mar 8, 2008 #6

    Eus

    User Avatar

    Hi Ho!

    Ah, you are right!
    Because what I did was to include a header file once in each source file (this was what I thought as multiple inclusion of a header file), I did not get any error.
    But, when I included a header file two times in a single source file like what you have illustrated, I got an error.

    Of course, the error is because redefinition of the struct. Whereas, multiple inclusion of a function prototype does not cause any error. Well, it is because the function prototype does not conflict each other.

    Now, I have the reason to avoid multiple inclusion of a header file at all times. For example, if in a large program foo.h has been included in bar.h and main.c includes foo.h and bar.h, an error will occur if foo.h contains something (e.g., struct, enum, and typedef) other than just some function-prototypes.
    Because struct, enum, and typedef declaration should be put in a header file, no precaution can be taken to make successful multiple inclusion of a header file. Therefore, multiple inclusion of a header file must be avoided at all times with
    Code (Text):

    #ifndef FOO_H
    #define FOO_H 1
    ...
    #endif
     
    construct.

    So, the question has been answered.

    Thank you very much.

    Best regards,
    Eus
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Why not multiple inclusion of a header file if certain precautions are taken?
Loading...