473,657 Members | 2,896 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 22 '05 #1
6 1746
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.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
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.sniff domain:
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?


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


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******@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

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
6424
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? What is the best followed approach?
2
1986
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. Consider this code that compiles correctly on Sun's Forte 7 compiler: #include <string> int main() { std::string text = "34"; std::atoi(text.c_str()); return 0;
31
2781
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 http://www.kdevelop.org http://www.suse.com http://www.mozilla.org
18
2243
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 the implementation of its member functions in foo.cpp (which obviously #inludes foo.h) and further assume that it depends on a class bar which is defined in bar.h. It seems that there are the following two scenarios:
18
2735
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 (collection of files) which are not required, except for one header file's contents. I'd say 'No, header files should be included in the C source, not in another
8
3046
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 structure sqlca is declared. i included this file as a soft link in /usr/include. the soft link is as follows: sqlca.h -> /usr/opt/db2_08_01/include64/sqlca.h
6
5200
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
2739
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 inclusion relationships. Another question I have is how to solve this dependency when "typedef" is used. usually we can use forward declaration like "struc A; " to solve references like "struct A* p". But in many places we have
4
2035
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 main.c. Now I create a header file which represents the interface of functions.c to my main program file main.c. I put in the header file: all function prototypes with keyword extern and the declarations of the structs, which are defined in the main...
0
8384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8302
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.