Header inclusion question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johannes Bauer

    #1

    Header inclusion question

    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.cp p" file could look like this

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

    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.keys erver.penguin.d e.
    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

    iD8DBQFBtigaCse FG8xyfi4RAj9uAJ 9beapHtzBnDJSYs lpeF++W78z1NgCf T8t8
    YZQ5lusdrq/Gdp+/Bjxyck8=
    =YuDA
    -----END PGP SIGNATURE-----

  • Victor Bazarov

    #2
    Re: Header inclusion question

    Johannes Bauer wrote:[color=blue]
    > 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.cp p" file could look like this
    >
    > #include "ClassC.hpp " // Needed by ClassB
    > #include "ClassB.hpp " // Needed by Example
    > #include "Example.hp p"
    >
    > 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[/color]

    What does this have to do with dependencies?
    [color=blue]
    > Yet I think this is quite ugly too...
    >
    > Are there other alternatives?[/color]

    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

    Comment

    • Andre Kostur

      #3
      Re: Header inclusion question

      Johannes Bauer <dfnsonfsduifb@ gmx.de> wrote in news:rplh82xip5 .ln2
      @snifftop.sniff domain:
      [color=blue]
      > 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.cp p" file could look like this
      >
      > #include "ClassC.hpp " // Needed by ClassB
      > #include "ClassB.hpp " // Needed by Example
      > #include "Example.hp p"
      >
      > 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?[/color]

      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.cp p" file with
      one line in it:

      #include "Example.hp p"

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

      Example.cpp:

      #include "Example.hp p"

      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(Cla ssA var);
      };

      #endif

      ClassA.hpp:

      #ifndef INC_CLASSA_HPP
      #define INC_CLASSA_HPP

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

      #endif


      Comment

      • E. Robert Tisdale

        #4
        Re: Header inclusion question

        Johannes Bauer wrote:
        [color=blue]
        > 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.cp p" file could look like this
        >
        > #include "ClassC.hpp " // Needed by ClassB
        > #include "ClassB.hpp " // Needed by Example
        > #include "Example.hp p"
        >
        > 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?[/color]

        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

        Comment

        • Lawrence Troxler

          #5
          Re: Header inclusion question

          Andre Kostur <nntpspam@kostu r.net> 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.cp p" file with
          : one line in it:

          : #include "Example.hp p"

          : 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.cp p" 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

          Comment

          • EventHelix.com

            #6
            Re: Header inclusion question

            Header file inclusion can be simplified by effective use of forward
            declarations. See the following article:

            http://www.eventhelix.com/RealtimeMa...dePatterns.htm

            Deepa
            --
            Sequence diagram based systems engineering and architecture design tool. Built in support for alternative scenarios and multi-tier architectures.

            EventStudio 2.5 - Generate sequence diagrams from plain text input

            Comment

            • EventHelix.com

              #7
              Re: Header inclusion question

              Header file inclusion can be simplified by effective use of forward
              declarations. See the following article:

              http://www.eventhelix.com/RealtimeMa...dePatterns.htm

              Deepa
              --
              Sequence diagram based systems engineering and architecture design tool. Built in support for alternative scenarios and multi-tier architectures.

              EventStudio 2.5 - Generate sequence diagrams from plain text input

              Comment

              Working...