473,785 Members | 2,669 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
18 2753
John Smith wrote:
What does the group think
"Group think" is an intellectual disease.
of the practise of including one header file from inside another?
It is essential.
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 practice?


Header files are files include'd by the C preprocessor
at the *head* of a C source file or another header file.
Files included elsewhere in the body of a source file
are not technically header files.

Header files are usually used to introduce *module interfaces*
into source programs. Such header files should be both

1. self-sufficient and
2. idempotent.

Self-sufficient means that the header file should include
all of the header files that it needs to compile properly.
Idempotent means that the content is protected by so-called
*guard macros"

#ifndef GUARD_FILE_H
#define GUARD_FILE_H 1
// contents
#endif//GUARD_FILE_H

that prevent the C preprocessor
from including the contents more than once.

Remember that an *interface* has no substance itself.
It contains function declarations, macro and type definitions
but nothing that would compel the compiler to emit code by itself.
Header files that effect module interfaces should *not* include
[global] function definitions!
This means that including unused interfaces should be *harmless* --
*no* dependencies should be created.
Nov 14 '05 #11
kal
Da*****@cern.ch (Dan Pop) wrote in message news:<cd******* ***@sunnews.cer n.ch>...
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? ;-)


No.
Nov 14 '05 #12
kal wrote:

Da*****@cern.ch (Dan Pop) wrote in message news:<cd******* ***@sunnews.cer n.ch>...
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? ;-)


No.


If the main file #includes header file a.h,
which #includes header file b.h, which #includes header file c.h,
which has a FILE type parameter in a function prototype,
and which also #includes stdio.h, then I may be inclined,
when using a function like printf in the main file,
to also #include stdio.h in the main file,
even though it isn't needed there.

The reason being because I may want to be able to change
a.c and a.h, without changing the #includes in the main file.

It would depend on whether or not the features in a.c, a.h, b.c, b.h
and c.c, which required the inclusion of stdio.h,
were fundamental to the main file.

--
pete
Nov 14 '05 #13
In <a5************ **************@ posting.google. com> k_*****@yahoo.c om (kal) writes:
Da*****@cern.c h (Dan Pop) wrote in message news:<cd******* ***@sunnews.cer n.ch>...
>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? ;-)


No.


Why? With few exceptions, no header *needs* to be included twice. And
these exceptions, obviously, don't have guards against multiple inclusion.

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

"John Smith" <so*****@micros oft.com> wrote in message
news:cd******** **@newstree.wis e.edt.ericsson. se...
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


What is this obsession with replying 'Don't forget the inclusion guard'???
It's old hat. My secnario has this in place.

My problem is one module is creating a dependancy on another module only
because the first module wants to use one single #define in the second
module.

The application is a large one, backed up by an overly sophisticated build
system, which would mean if I want to pick up module 2, I pick up all of it:
it all gets compiled. (And yes, I know: it won't get linked in!).

So the question is: is it normal practise to create intermodule dependancies
for trivial reasons?
Nov 14 '05 #15

On Thu, 22 Jul 2004, John Smith wrote:

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

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 [...]
What is this obsession with replying 'Don't forget the inclusion guard'???
It's old hat. My secnario has this in place.
Good. Many scenarios don't; or almost as bad, have incorrect header
guards, something like this picture. (A slightly interesting puzzle
is to count the number of mistakes in this header!)

#ifndef _MYHEADER_H
#define _MYHEADER_H

#ifdef __CPLUSPLUS__
extern 'C' {
#endif

#ifndef _STDIO_H
#include <stdio.h>
#endif

void foo(size_t bar);

#ifdef __CPLUSPLUS__
}
#endif
#endif

My problem is one module is creating a dependancy on another module only
because the first module wants to use one single #define in the second
module.
Well, if you use a macro that's #defined in another module, that would
be a dependency, yes. I think we all assumed that was clear.
The application is a large one, backed up by an overly sophisticated build
system, which would mean if I want to pick up module 2, I pick up all of it:
it all gets compiled. (And yes, I know: it won't get linked in!).
I would guess that very few people on this newsgroup have any experience
with your "overly sophisticated build system." Plain vanilla 'make' is
smart enough for many projects. And if a dependency on a /header file/
is causing any noticeable increase in compile time, I would say that your
build system has a design flaw.
So the question is: is it normal practise to create intermodule
dependancies for trivial reasons?


What counts as "trivial"? Most of my /programs/ are written for trivial
reasons (ranging from "because I want it" to "because I get paid to do it"
to "because I'm bored"). It sounds like you think this dependency is a
bad idea. Can you remove it without causing other bad side effects
(unmaintainabil ity, making the chief programmer jealous, breaking the
build tool)? If so, why don't you just go ahead and /do/ it?

Ask a vague question, get a vague answer.

-Arthur,
about seven
Nov 14 '05 #16
Hi John,

What is this obsession with replying 'Don't forget the inclusion guard'???
It's old hat. My secnario has this in place.
Right. Not everyone told you this but you got answers to your question
as well.
The reason you get the "old hat" is that your question is vague enough
that we cannot be sure whether you are aware of the safe include
mechanism and the errors forgetting it can cause.

My problem is one module is creating a dependancy on another module only
because the first module wants to use one single #define in the second
module.
Yes. You got answers to that. I gave one, people with more clue gave you
other answers.

The application is a large one, backed up by an overly sophisticated build
system, which would mean if I want to pick up module 2, I pick up all of it:
it all gets compiled. (And yes, I know: it won't get linked in!).

So the question is: is it normal practise to create intermodule dependancies
for trivial reasons?


Try to ask clearer questions, please. "Trivial" is not clear in this
context. If the two settings I described are trivial, then: yes, if
one is not: no.

Hint: Filter out the static (that is: the inclusion guard stuff),
have a look at the remaining answers, try to come up with smart
questions based on these answers. Most of us certainly do not want to
frustrate you (having better things to do with their time).
Please try not to frustrate us in turn :-)
Cheers,
Michael

Nov 14 '05 #17

"Michael Mair" <ma************ ********@ians.u ni-stuttgart.de> wrote in
message news:cd******** **@infosun2.rus .uni-stuttgart.de...
Hi John,

What is this obsession with replying 'Don't forget the inclusion guard'??? It's old hat. My secnario has this in place.


Right. Not everyone told you this but you got answers to your question
as well.
The reason you get the "old hat" is that your question is vague enough
that we cannot be sure whether you are aware of the safe include
mechanism and the errors forgetting it can cause.

My problem is one module is creating a dependancy on another module only
because the first module wants to use one single #define in the second
module.


Yes. You got answers to that. I gave one, people with more clue gave you
other answers.

The application is a large one, backed up by an overly sophisticated build system, which would mean if I want to pick up module 2, I pick up all of it: it all gets compiled. (And yes, I know: it won't get linked in!).

So the question is: is it normal practise to create intermodule dependancies for trivial reasons?


Try to ask clearer questions, please. "Trivial" is not clear in this
context. If the two settings I described are trivial, then: yes, if
one is not: no.

Hint: Filter out the static (that is: the inclusion guard stuff),
have a look at the remaining answers, try to come up with smart
questions based on these answers. Most of us certainly do not want to
frustrate you (having better things to do with their time).
Please try not to frustrate us in turn :-)
Cheers,
Michael

Sorry if I have frustrated you all, it certainly wasn't my intention.

Thanks for all the replies. I'll figure something out of it all, I'm sure.

JS
Nov 14 '05 #18
On Thu, 22 Jul 2004 14:20:19 +0100, "John Smith"
<so*****@micros oft.com> wrote in comp.lang.c:

"John Smith" <so*****@micros oft.com> wrote in message
news:cd******** **@newstree.wis e.edt.ericsson. se...
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


What is this obsession with replying 'Don't forget the inclusion guard'???
It's old hat. My secnario has this in place.

My problem is one module is creating a dependancy on another module only
because the first module wants to use one single #define in the second
module.

The application is a large one, backed up by an overly sophisticated build
system, which would mean if I want to pick up module 2, I pick up all of it:
it all gets compiled. (And yes, I know: it won't get linked in!).

So the question is: is it normal practise to create intermodule dependancies
for trivial reasons?


In my opinion, a source file containing two single lines must compile
without diagnostics, or with only a diagnostic concerning the failure
of the translation unit to contain any external declarations:

#include "required_proje ct_header.h"
#include "header_in_ques tion.h"

....where "required_proje ct_header.h" is just that, required to be the
first header included in every C source file in the project. It in
turn includes at least <stdint.h> and <stddef.h>, plus generally some
project-specific headers with universally used typedef's and macros.

In setting up the rules for our latest large C embedded project, I
tried to eliminate some of the more obvious disadvantages of earlier
project structures. The biggest of these was "header dependency
hell".

In earlier projects, functions and data types that provided interfaces
between various modules of the program were all placed in a single
header for each module, named xxx_api.h for module xxx.

The problem is that xxx_api.h almost always needed to include some
type defined by a different module in some structure of its own, or
accept or return one (or a pointer to one) in a function call.

So xxx_api.h required the inclusion of yyy_api.h, which in turn needed
something from zzz_api.h, and in the end way too many files ended up
including most or all of the API headers.

The obvious solution, which has reduced these dependencies greatly, is
to require two headers for things exported by each module:

xxx_api_types.h
xxx_api_calls.h

The latter, of course, must include the former. A few simple rules:

1. C source files may only include *_calls.h files, and never any
*_types.h, other than that belonging to its own module.

2. *.api_calls.h files must include their own api_types.h file, and
may include other *api_types.h files, but may not include any other
api_calls.h files.

This has cut the header coupling down significantly.

If you are working with legacy code, and can't completely reorganize
the code base, consider moving the one dependency to a header file of
its own:

some_header_nam e.h:

#ifndef THIS_IS_DEFINED _IN_HEADER2
#define THIS_IS_DEFINED _IN_HEADER2 5
#endif

Include this header in the big include file in place of the
definition, and include it in other places that only needed this
symbol from the big include file.

This is the sort of thing that is a good candidate for inclusion in
"required_proje ct_header.h".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #19

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

Similar topics

3
5134
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
6433
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
2094
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
2807
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
3098
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
5611
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
8324
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
9645
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
10147
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...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
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
6739
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
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
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.