Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Header inclusion question

Reply
Thread Tools

Header inclusion question

 
 
Johannes Bauer
Guest
Posts: n/a
 
      12-07-2004
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-----

 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      12-07-2004
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
 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      12-07-2004
Johannes Bauer <(E-Mail Removed)> 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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-07-2004
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
 
Reply With Quote
 
Lawrence Troxler
Guest
Posts: n/a
 
      12-08-2004
Andre Kostur <(E-Mail Removed)> 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

 
Reply With Quote
 
EventHelix.com
Guest
Posts: n/a
 
      12-08-2004
Header file inclusion can be simplified by effective use of forward
declarations. See the following article:

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

Deepa
--
http://www.EventHelix.com/EventStudio
EventStudio 2.5 - Generate sequence diagrams from plain text input

 
Reply With Quote
 
EventHelix.com
Guest
Posts: n/a
 
      12-08-2004
Header file inclusion can be simplified by effective use of forward
declarations. See the following article:

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

Deepa
--
http://www.EventHelix.com/EventStudio
EventStudio 2.5 - Generate sequence diagrams from plain text input

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in inclusion of header files vector.h,map.h.. kpoan C++ 0 08-15-2006 03:38 AM
tool to check multiple inclusion of header file techBoy C Programming 6 03-15-2006 10:49 AM
C++ Header Inclusion Matthew Burgess C++ 2 08-28-2003 08:16 PM
[C++] Order of header inclusion qazmlp C++ 9 08-25-2003 05:12 AM
Header Inclusion style qazmlp C Programming 14 07-10-2003 11:41 AM



Advertisments