a .cpp file always include the corresponding header file first

Discussion in 'C' started by princemaozh, Jan 14, 2008.

  1. princemaozh

    princemaozh New Member

    Joined:
    Aug 28, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    In many articles, there is such a coding guideline:

    >> On a .cpp file always include the corresponding header file first, like this:
    Code:
    // This is Foo.cpp
     
    #include "Foo.h"  
    #include "tao/Bar.h" 
    #include "ace/Baz.h" 
     
    // Here comes the Foo.cpp code....
    
    >> In this way we are sure that the header file is self-contained and can be safely included from some place else.

    Why it can make sure the header file is self-contained by including the header file first in the corresponding implementation file? Can anybody explain it a little bit more?
     
    Last edited: Jan 15, 2008
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    By making sure that foo.h includes gromit.h and widget.h automatically, you save having to explain to all users of foo.h the need to include the other two files, and save having to remind them should they accidentally forget.

    If at some future time, foo.h removed gromit.h and adds wallace.h, then all the users of foo.h don't have to go round changing many thousands of instances of including foo.h to fix the pre-requisite headers.
     
  3. princemaozh

    princemaozh New Member

    Joined:
    Aug 28, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Salem, thanks for your answer. But it seems you explained the advantage of making the header file "self-contained".

    My question is:
    Why "a .cpp file always include the corresponding header file first (see example above)" is able to check whether the header file is self-contained?
    What will happen if I include other header files before the corresponding header file of that .cpp file?
     
  4. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    By being included first is how it checks.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    No Difference. It does not depend on the order of file inclusion but need to include before you first need to use them.
     
  6. princemaozh

    princemaozh New Member

    Joined:
    Aug 28, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Salem, do you mean if the cpp file does not include the corresponding header file in the first line (e.g. include other .h files, then its corresponding header file), it has no chance to check whether the corresponding header file is self-contained?

    Shabbir, seems your answer conflicts with Salem's.

    I get a little confused.
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I could not get the meaning of the post you are quoting though.
     
  8. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    Well if you had this
    Code:
    #include "other.h"
    #include "myHeader.h"
    
    Then there may be the possibility that myHeader.h depends on other.h in some way.

    But by having this
    Code:
    #include "myHeader.h"
    #include "other.h"
    
    There is no possibility of a hidden dependency.
    Either myHeader.h doesn't depend on other.h, or it does depend on other.h and it (myHeader.h) includes other.h as well.

    So by including it first, you guarantee that it has no hidden dependencies (otherwise it would fail to compile). This gives confidence that anyone else who needs myHeader.h can include it without having to worry about dependencies.
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    So there is no conflict now because what I said was syntactically correct and what Salem is saying is what you should be doing for better understanding of code.
     
  10. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    use
    #pragma once

    In case of to avoid too many times including the same header file
     
  11. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    Macro guards are the preferred method for ensuring single inclusion.
    It is more portable and does not have the problems of #pragma once
    (cannot know that the same file is included under two different names
    as can occur with links on unix, etc.).
    gcc considers #pragma once to be deprecated.
    These are macro (or include) guards:
    Code:
    // header.h
    #ifndef HEADER_H
    #define HEADER_H
    ...
    #endif
     
  12. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    In pragma once same code is written as you have written.
     
  13. princemaozh

    princemaozh New Member

    Joined:
    Aug 28, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Salem, thank you very much for the explanation.

    Shabbir and others, also thanks for your comments.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice