473,769 Members | 4,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Header files included in header files

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
header', but I've always come across strong arguments for the latter.

What do you think, and what is accepted practise?

Thanks
JS
Nov 14 '05 #1
18 2751
In article <cd**********@n ewstree.wise.ed t.ericsson.se>,
"John Smith" <so*****@micros oft.com> wrote:
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
header', but I've always come across strong arguments for the latter.

What do you think, and what is accepted practise?


If you write a header file "header.h", then it must be written in such a
way that the single line source file

#include "header.h"

compiles without errors. That means header.h needs to include everything
that is necessary to make this work, but not more.

Note that a function can have arguments or return values of type "struct
xxx *" without having the declaration of struct xxx around; this feature
of C often makes it unnecessary to include header files within header
files.
Nov 14 '05 #2

"Christian Bau" <ch***********@ cbau.freeserve. co.uk> wrote in message
news:ch******** *************** **********@slb-newsm1.svr.pol. co.uk...
In article <cd**********@n ewstree.wise.ed t.ericsson.se>,
"John Smith" <so*****@micros oft.com> wrote:
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 header', but I've always come across strong arguments for the latter.

What do you think, and what is accepted practise?


If you write a header file "header.h", then it must be written in such a
way that the single line source file

#include "header.h"

compiles without errors. That means header.h needs to include everything
that is necessary to make this work, but not more.

Note that a function can have arguments or return values of type "struct
xxx *" without having the declaration of struct xxx around; this feature
of C often makes it unnecessary to include header files within header
files.


Thanks Christian

You have a good point about it having to compile. This particular header
problem has, in the first header, something like
typedef struct
{ int array[THIS_IS_DEFINED _IN_HEADER2]; } astruct;

and of course in the other header
#define THIS_IS_DEFINED _IN_HEADER2 5

This turns out to be the only dependant thing!
Normal practise, or poor coding?

Thanks
JS
Nov 14 '05 #3
Hi John,

You have a good point about it having to compile. This particular header
problem has, in the first header, something like
typedef struct
{ int array[THIS_IS_DEFINED _IN_HEADER2]; } astruct;

and of course in the other header
#define THIS_IS_DEFINED _IN_HEADER2 5

This turns out to be the only dependant thing!
Normal practise, or poor coding?


It depends. I am working with a huge code where you have hundreds
of header files. Many of the THIS_IS_DEFINED _IN_HEADER2 type
symbolic constants make sense insofar as you then can assume
that all arrays (in your example) serving a certain purpose
are of the same size -- the advantages are clear, I think.

On the other hand, if this symbolic constant does not have
a "deeper" meaning for the purpose of your array but only has
per chance the value 5 which you also have as the array size,
then it is poor coding.

If you are sure about the implications, you can also use a
construct like

#ifndef DONTWANNAINCLUD EIT
#include "header2.h"
#endif

#ifdef THIS_IS_DEFINED _IN_HEADER2
#define MY_ARRAY_SIZE THIS_IS_DEFINED _IN_HEADER2
#else
#define MY_ARRAY_SIZE 5
#endif

typedef struct {
int array[MY_ARRAY_SIZE];
} astruct;
Cheers,
Michael

Nov 14 '05 #4
John Smith a couché sur son écran :
What does the group think of the practise of including one header file from
inside another?
This group is made of individuals fitted with free will (well, I guess)
and free speech (this, I know!). It may have some consensus on certain
points, but don't expect some 'unique way of thinking' from this group.
It's not a sectarian group. It's an open and free community. Expression
is individual.
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.
Sounds like a design issue.
I'd say 'No, header files should be included in the C source, not in another
header', but I've always come across strong arguments for the latter.


Heavy rules are raley useful. Better to act according to the situation.

That said, here are some basics (call them rules if you want):

- A header should be guarded against multiple inclusions.
- A header should hold definitions of macros, types and structures [1]
and declarations of functions (in prototyped form) and objects.
- A header sould be included when needed. Not less, not more.

------------------
[1] and the definitions of inline functions in C99 or more.
And some more general recommendations :

- The code should be organized into individual functionnal blocks with
a clear public interface.
- The code should be unit testable.
- The public interface is defined in a header file that belongs to the
functionnal block.

This is an important design guideline that helps to make solid,
efficient and reusable code.

--
Emmanuel

Nov 14 '05 #5
In <cd**********@n ewstree.wise.ed t.ericsson.se> "John Smith" <so*****@micros oft.com> writes:

You have a good point about it having to compile. This particular header
problem has, in the first header, something like
typedef struct
{ int array[THIS_IS_DEFINED _IN_HEADER2]; } astruct;

and of course in the other header
#define THIS_IS_DEFINED _IN_HEADER2 5

This turns out to be the only dependant thing!
Normal practise, or poor coding?


It entirely depends on the application design. It is common practice
to have all the user-serviceable parts defined together in a single
header and all the other headers needing macros defined in that header
will have to include it. Ditto for special typedef's required by the
application.

IMHO, in a complex application with a well structured set of headers,
higher level headers will have to include lower level headers.

Of curse, I cannot make any comments WRT the design of your application.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
In <mn************ ***********@YOU RBRAnoos.fr> Emmanuel Delahaye <em***@YOURBRAn oos.fr> writes:
Heavy rules are raley useful. Better to act according to the situation.
I certainly hope that Emmanuel believes what he wrote above. (They're
called "strong rules", BTW.)
That said, here are some basics (call them rules if you want):

- A header should be guarded against multiple inclusions.
- A header should hold definitions of macros, types and structures [1]
and declarations of functions (in prototyped form) and objects.
- A header sould be included when needed. Not less, not more.

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
Doesn't this rule render the first one superfluous? ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7

On Wed, 21 Jul 2004, Christian Bau wrote:

"John Smith" <so*****@micros oft.com> wrote:

What does the group think of the practise of including one header
file from inside another?
If you write a header file "header.h", then it must be written in such a
way that the single line source file

#include "header.h"

compiles without errors. That means header.h needs to include everything
that is necessary to make this work, but not more.
(Furthermore, it must be written in such a way that the /two-line/
source file

#include "header.h"
#include "header.h"

compiles without errors. That means header.h needs to include header
guards in the proper places.)
Note that a function can have arguments or return values of type "struct
xxx *" without having the declaration of struct xxx around; this feature
of C often makes it unnecessary to include header files within header
files.


I wouldn't consider that good practice, though. If "header.h"
uses anything declared in "foo.h", then "header.h" ought to #include
"foo.h" on general principles, so that the maintainer can see the
dependency --- even if the compiler might not care. After all, you
certainly wouldn't want

==foo.h==

struct Foo {
int i;
};

==header.h==

struct Foo *hello(void);

==bar.h==

struct Foo {
int i[1024];
};

==myprog.c==

#include "bar.h"
#include "header.h"

when the implementation of 'hello()' was expecting to return a
pointer to the type defined in "foo.h"! Better to include all
dependencies, and shake out the bugs early.

-Arthur
Nov 14 '05 #8
On Wed, 21 Jul 2004 09:19:04 +0100, Christian Bau
<ch***********@ cbau.freeserve. co.uk> wrote:
In article <cd**********@n ewstree.wise.ed t.ericsson.se>,
"John Smith" <so*****@micros oft.com> wrote:
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
header', but I've always come across strong arguments for the latter.

What do you think, and what is accepted practise?
If you write a header file "header.h", then it must be written in such a
way that the single line source file

#include "header.h"


Agreed.
compiles without errors. That means header.h needs to include everything
that is necessary to make this work, but not more.

Note that a function can have arguments or return values of type "struct
xxx *" without having the declaration of struct xxx around; this feature
of C often makes it unnecessary to include header files within header
files.


Unnecessary, perhaps, but highly recommended nonetheless. There's no
good reason not to include the headers, and they should be there for
the benefit of the reader and so that the compiler can check the usage
of the function.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #9
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes:
On Wed, 21 Jul 2004, Christian Bau wrote:
"John Smith" <so*****@micros oft.com> wrote:

What does the group think of the practise of including one header
file from inside another?

If you write a header file "header.h", then it must be written in such a
way that the single line source file

#include "header.h"

compiles without errors. That means header.h needs to include everything
that is necessary to make this work, but not more.


(Furthermore, it must be written in such a way that the /two-line/
source file

#include "header.h"
#include "header.h"

compiles without errors. That means header.h needs to include header
guards in the proper places.)


For certain values of "must". The #include directive just (in effect)
dumps the contents of the named header or file into your source at the
specified place. The language imposes few requirements on the content
of the header or the manner in which it's included, as long as the
final result is legal C. If "header.c" contains the line

i++;

then obviously the placement and number of any #include directives
will be significant, and the compiler is unlikely to complain.

But of course that kind of thing is horrendously bad style and is
shunned by all right-thinking programmers.

Which is a long-winded way of saying that I agree entirely, except
that I would have written "should" rather than "must". The meta-point
is that it's a matter of style (which of course doesn't make it
unimportant).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

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

Similar topics

3
5133
by: dharmesh Gupta | last post by:
Hi All, i am new to header files , and have a basic question , i have made my program in three files namely, idr_bpl.cpp( contains class definitions and function definitions) bpl.cpp( contains main() function) bpl.h (contains variable definition of a variable that is shared in the above two files )
8
6431
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?
7
2092
by: header_question | last post by:
Is it best to place all #include files in one header file seperate from all the other files in a project? What is the logic that determines the order of the #include files, does it depend on the order of function calls in main?
31
2806
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
16
12549
by: matthurne | last post by:
I just started learning C++ on my own...I'm using Accelerated C++. Something it hasn't explained and I keep wondering about is how header files actually work. I suspect it doesn't get into it because it is, as the authors love to say, "implementation specific". If that's the case, how does the compiler commonly handle them? I use Linux and gcc specifically. Basically, I don't understand how a header file being included makes a...
11
2763
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do understand the objective. This example may represent an interface in need of a bit of refactoring, but it goes to demonstrate the basic idea as I understand it. http://developer.kde.org/documentation/library/cvs-api/kdevelop/html/ast_8h-source.html...
3
3097
by: pooja | last post by:
Suppose i have created a class c1 with f1()in c1.cpp and included this c1.cpp in file1.cpp file , which is also having main() by giving the statement #include "c1.cpp". the same i can do by using header file. i can create a class c1 with f1() in c1.h and include this c1.h in file1.cpp by giving the statement #include "c1.h" tell me that what exactly is the difference between c1.h and c1.cpp? Since they both are doing the same things.
11
5607
by: ambika | last post by:
Iam just trying to know "c". And I have a small doubt about these header files. The header files just contain the declaration part...Where is the definition for these declarations written??And how does that get linked to our program when we run it??I would appreciate any helpful info..And I would like to thank you for that in advance -ambika
60
8317
by: Derrick Coetzee | last post by:
It seems like, in every C source file I've ever seen, there has been a very definite include order, as follows: - include system headers - include application headers - include the header associated with this source file For example, in a file hello.c: #include <stdio.h>
0
9589
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
10219
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
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
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...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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
5310
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
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.