Header inclusion question

Discussion in 'C++' started by Johannes Bauer, Dec 7, 2004.

  1. Hi group,

    I've got a question concerning inclusion of .hpp files. Currently I'm
    including all needed header files in the .cpp file. This means all
    dependencies of the package and all dependencies of these dependencies
    and so on.

    This is quite ugly.

    A start of an "Example.cpp" file could look like this

    #include "ClassC.hpp" // Needed by ClassB
    #include "ClassB.hpp" // Needed by Example
    #include "Example.hpp"

    Now what I could do would include the dependencies of dependencies in
    the .hpp files and do a

    #ifndef EXAMPLE_H
    #define EXAMPLE_H
    ....
    #endif

    Yet I think this is quite ugly too...

    Are there other alternatives?

    Greetings,
    Johannes

    --
    PLEASE verify my signature. Some forging troll is claiming to be me.
    My GPG key id is 0xCC727E2E (dated 2004-11-03). You can get it from
    wwwkeys.pgp.net or random.sks.keyserver.penguin.de.
    Also: Messages from "Comcast Online" are ALWAYS forged.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.6 (GNU/Linux)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQFBtigaCseFG8xyfi4RAj9uAJ9beapHtzBnDJSYslpeF++W78z1NgCfT8t8
    YZQ5lusdrq/Gdp+/Bjxyck8=
    =YuDA
    -----END PGP SIGNATURE-----
     
    Johannes Bauer, Dec 7, 2004
    #1
    1. Advertising

  2. Johannes Bauer wrote:

    > I've got a question concerning inclusion of .hpp files.
    > Currently, I'm including all needed header files in the .cpp file.
    > This means all dependencies of the package
    > and all dependencies of these dependencies and so on.
    >
    >
    >
    > This is quite ugly.
    >
    > A start of an "Example.cpp" file could look like this
    >
    > #include "ClassC.hpp" // Needed by ClassB
    > #include "ClassB.hpp" // Needed by Example
    > #include "Example.hpp"
    >
    > Now what I could do would include the dependencies of dependencies in
    > the .hpp files and do a
    >
    > #ifndef GUARD_EXAMPLE_H
    > #define GUARD_EXAMPLE_H 1
    > // contents of header file
    > #endif//GUARD_EXAMPLE_H
    >
    > Yet I think this is quite ugly too...
    >
    > Are there other alternatives?


    Your header files appear to be module interfaces.
    Each such header file should be both

    1. idempotent and
    2. self-sufficient.

    Use "guard" macros as shown above
    to ensure that the contents of your header file
    is parsed only once by the C preprocessor.

    Each header file should include *all*
    od the header files that it requires.
    This means that, in this case,
    the only header file that you need to include
    in Example.cpp is Example.hpp
     
    E. Robert Tisdale, Dec 7, 2004
    #2
    1. Advertising

  3. Johannes Bauer

    Andre Kostur Guest

    Johannes Bauer <> wrote in news:rplh82xip5.ln2
    @snifftop.sniffdomain:

    > Hi group,
    >
    > I've got a question concerning inclusion of .hpp files. Currently I'm
    > including all needed header files in the .cpp file. This means all
    > dependencies of the package and all dependencies of these dependencies
    > and so on.
    >
    > This is quite ugly.
    >
    > A start of an "Example.cpp" file could look like this
    >
    > #include "ClassC.hpp" // Needed by ClassB
    > #include "ClassB.hpp" // Needed by Example
    > #include "Example.hpp"
    >
    > Now what I could do would include the dependencies of dependencies in
    > the .hpp files and do a
    >
    > #ifndef EXAMPLE_H
    > #define EXAMPLE_H
    > ...
    > #endif
    >
    > Yet I think this is quite ugly too...
    >
    > Are there other alternatives?


    IMHO, each header file should be able to be compiled on its own. Each
    header should also have its own include guards. So in your case, for
    each of your header files I should be able to create a .cpp file in which
    the only thing it does is #include that header file, that .cpp file
    should compile.

    So for your example I should be able to create an "Example.cpp" file with
    one line in it:

    #include "Example.hpp"

    and I should be able to compile the Example.cpp file without any
    problems. In your case that would mean

    Example.cpp:

    #include "Example.hpp"

    Example.hpp:

    #ifndef INC_EXAMPLE_HPP
    #define INC_EXAMPLE_HPP

    #include "ClassB.hpp"

    void somefn(ClassB var);

    #endif

    ClassB.hpp:

    #ifndef INC_CLASSB_HPP
    #define INC_CLASSB_HPP

    class ClassB
    {
    public:
    void someotherfn(ClassA var);
    };

    #endif

    ClassA.hpp:

    #ifndef INC_CLASSA_HPP
    #define INC_CLASSA_HPP

    class ClassA
    {
    public:
    void somethirdfn();
    };

    #endif
     
    Andre Kostur, Dec 7, 2004
    #3
  4. Johannes Bauer wrote:
    > I've got a question concerning inclusion of .hpp files. Currently I'm
    > including all needed header files in the .cpp file. This means all
    > dependencies of the package and all dependencies of these dependencies
    > and so on.
    >
    > This is quite ugly.
    >
    > A start of an "Example.cpp" file could look like this
    >
    > #include "ClassC.hpp" // Needed by ClassB
    > #include "ClassB.hpp" // Needed by Example
    > #include "Example.hpp"
    >
    > Now what I could do would include the dependencies of dependencies in
    > the .hpp files and do a
    >
    > #ifndef EXAMPLE_H
    > #define EXAMPLE_H
    > ...
    > #endif


    What does this have to do with dependencies?

    > Yet I think this is quite ugly too...
    >
    > Are there other alternatives?


    The Only Right Way is to include only the headers that are needed to
    resolve symbols in _that_ module (whether it's a translation unit or
    another header).

    If your 'ClassB.hpp' needs 'ClassC.hpp', then 'ClassB.hpp' should include
    it, not 'Example.cpp'. If your 'Example.hpp' needs 'ClassB.hpp', then it
    itself should include 'ClassB.hpp'.

    Now, it's a different story when we start talking _needing_ to include
    anything. And you can reduce the includes by forward-declaring as much as
    possible. For example, if your class definition only declares the use of
    some class by a pointer or a reference, there is no need for the compiler
    to know the other class' definition.

    Victor
     
    Victor Bazarov, Dec 7, 2004
    #4
  5. Andre Kostur <> wrote:

    : IMHO, each header file should be able to be compiled on its own.

    Yes.

    : So for your example I should be able to create an "Example.cpp" file with
    : one line in it:

    : #include "Example.hpp"

    : and I should be able to compile the Example.cpp file without any
    : problems.

    Why go through all this trouble? With good development tools, you should
    be able to directly compile the header file.
    There should be no need to create this "Example.cpp" just for testing purposes.

    Of course, there are probably no "good development tools" in this context.
    In a certain Microsoft Windows GUI C++ development environment, for
    example, it doesn't work from the GUI - you can't simply open a header file
    in the IDE editor and compile it.

    Larry
     
    Lawrence Troxler, Dec 8, 2004
    #5
  6. EventHelix.com, Dec 8, 2004
    #6
  7. EventHelix.com, Dec 8, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    9
    Views:
    541
    Andre Kostur
    Aug 25, 2003
  2. Matthew Burgess

    C++ Header Inclusion

    Matthew Burgess, Aug 28, 2003, in forum: C++
    Replies:
    2
    Views:
    2,088
    Mike Smith
    Aug 28, 2003
  3. qazmlp

    Header Inclusion style

    qazmlp, Jul 8, 2003, in forum: C Programming
    Replies:
    14
    Views:
    555
    Casper H.S. Dik
    Jul 10, 2003
  4. kpoan
    Replies:
    0
    Views:
    482
    kpoan
    Aug 15, 2006
  5. techBoy
    Replies:
    6
    Views:
    338
    Vladimir S. Oka
    Mar 15, 2006
Loading...

Share This Page