473,382 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.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-----

Jul 22 '05 #1
6 1723
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
Jul 22 '05 #2
Johannes Bauer <df***********@gmx.de> 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
Jul 22 '05 #3
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
Jul 22 '05 #4
Andre Kostur <nn******@kostur.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.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

Jul 22 '05 #5
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

Jul 22 '05 #6
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

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: qazmlp | last post by:
I need to include a list of - C++ headers - headers of other modules - headers of my module - implementation specific ie.OS headers In what order, they should be included in my .CPP file?...
2
by: Matthew Burgess | last post by:
According to 17.4.4.1 paragraph 1 in the standard "A C++ header may include other C++ headers." This gives rise to what I consider to be a portability problem, albeit trivially corrected. ...
31
by: Steven T. Hatton | last post by:
If a header is not necessarily a source file, and the sequences delimited by < and > in header names aren't necessarily valid source file names, what exactly is a header? -- p->m == (*p).m == p.m...
18
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and...
18
by: John Smith | last post by:
Hi all What does the group think of the practise of including one header file from inside another? I have some legacy code where this has been done, and it creates a dependency on a module...
8
by: nrhayyal | last post by:
Hi c++ Gurus, Need your blessing. while testing few aspects with respect to header file inclusions, i observed few things which i would like to share with you. i have a file sqlca.h in which a...
6
by: techBoy | last post by:
I am looking for a tool that can scan my soyrce code and check if a header file gets included more then once in a sequece of compiled code. Can some one guide me to such a tool !!
3
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex...
4
by: Christoph Scholtes | last post by:
Hi, I have some questions about header files: Say I have a file functions.c which contains a couple of functions. I have declared some structs in this file too. The structs are defined in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.