473,385 Members | 2,162 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,385 software developers and data experts.

include dependecies--best practice?

I read recently (can't remember if it was on this
group or elsewhere) that it is a bad idea to write
a header file this way:

#ifndef FOO_HDR
#define FOO_HDR 1

#include <stdio.h>

int foo(FILE *i);
#endif

Instead, one should not rely on the header guards
in the included library and require that the program
that includes foo.h include stdio.h first.

Is that true? If so, why?

--
Bill Pursell

Aug 4 '06 #1
7 2351


Bill Pursell wrote On 08/04/06 14:51,:
I read recently (can't remember if it was on this
group or elsewhere) that it is a bad idea to write
a header file this way:

#ifndef FOO_HDR
#define FOO_HDR 1

#include <stdio.h>

int foo(FILE *i);
#endif

Instead, one should not rely on the header guards
in the included library and require that the program
that includes foo.h include stdio.h first.

Is that true? If so, why?
This is Question 10.7 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.c-faq.com/

--
Er*********@sun.com

Aug 4 '06 #2

Eric Sosman wrote:
Bill Pursell wrote On 08/04/06 14:51,:
I read recently (can't remember if it was on this
group or elsewhere) that it is a bad idea to write
a header file this way:

#ifndef FOO_HDR
#define FOO_HDR 1

#include <stdio.h>

int foo(FILE *i);
#endif

Instead, one should not rely on the header guards
in the included library and require that the program
that includes foo.h include stdio.h first.

Is that true? If so, why?

This is Question 10.7 in the comp.lang.c Frequently
Asked Questions (FAQ) list
Unfortunately, the FAQ doesn't really answer the question
other than to say it's a stylistic question. However, it
did make me realize that it was in the Indian Hill Style
guide that I'd read the opinion that it was a bad idea
to include the necessary headers, as it relies on
the headers being properly guarded. However,
the Indian Hill style guide is now 16 years old...
is there a more definitive answer? (ie, can the FAQ
be updated to say: "Go ahead and include what you
need: current implementations have the proper
header guards in place.")

My choice is to include everything you need. I'd
love to hear the reasons of people who think that's
a bad idea.

--
Bill Pursell

Aug 4 '06 #3


Bill Pursell wrote On 08/04/06 16:20,:
Eric Sosman wrote:
>>Bill Pursell wrote On 08/04/06 14:51,:
>>>I read recently (can't remember if it was on this
group or elsewhere) that it is a bad idea to write
a header file this way:

#ifndef FOO_HDR
#define FOO_HDR 1

#include <stdio.h>

int foo(FILE *i);
#endif

Instead, one should not rely on the header guards
in the included library and require that the program
that includes foo.h include stdio.h first.

Is that true? If so, why?

This is Question 10.7 in the comp.lang.c Frequently
Asked Questions (FAQ) list


Unfortunately, the FAQ doesn't really answer the question
other than to say it's a stylistic question. [...]
It does a bit more than that: It also lays out the
principal arguments on each side.
My choice is to include everything you need. I'd
love to hear the reasons of people who think that's
a bad idea.
If you've got a new argument (for or against), by all
means arise it and let's have a discussion. But if you
just want to hear the same old arguments re-orated for your
amusement, ... I fear you'll probably get your wish.

"LESS FILLING!!"

"TASTES GREAT!!!"

--
Er*********@sun.com

Aug 4 '06 #4
Bill Pursell wrote:
I read recently (can't remember if it was on this
group or elsewhere) that it is a bad idea to write
a header file this way:

#ifndef FOO_HDR
#define FOO_HDR 1

#include <stdio.h>

int foo(FILE *i);
#endif
I don't understand, at all, how one could possibly object to this.
There is an obvious plus for doing this: it allows libraries to have
interdependencies in their interface without the programmer having to
account for these in their list of #includes.

So an obvious example -- lets say the whole API of module A depends on
typedefs from module B. Now in your mainline code you need just A, but
not the parts of the API which use tye typedefs from B. Intuitively
you decide you just want to include A's include file. Now if the A
include file did not also intrinsically include the B file then you
would be *forced* to include the B include file in your mainline code
*before* including the A include file. So the mainline code is now
including a file, even though you might find no direct usage of any of
its contents inside your mainline code.

Using this header guarding trick basically allows you to always declare
dependencies on a file by file basis and to avoid *hidden* dependencies
such as "include file order" dependencies. Unfortunately it allows you
to abuse "implicit dependencies" (relying on the fact that another
module will automatically include some other include file for you, so
that you can avoid including it yourself) but those usually make
themselves clear very easily as dependencies change and usually don't
result in any severe negative impact.

If that's not convincing enough for you, go look at the include files
from your compiler. Every compiler I have access to uses precisely
this sort of trick in their include files.
Instead, one should not rely on the header guards
in the included library and require that the program
that includes foo.h include stdio.h first.

Is that true? If so, why?
I don't see how it could possible be true.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Aug 4 '06 #5
On 4 Aug 2006 13:20:36 -0700, "Bill Pursell" <bi**********@gmail.com>
wrote in comp.lang.c:
>
Eric Sosman wrote:
Bill Pursell wrote On 08/04/06 14:51,:
I read recently (can't remember if it was on this
group or elsewhere) that it is a bad idea to write
a header file this way:
>
#ifndef FOO_HDR
#define FOO_HDR 1
>
#include <stdio.h>
>
int foo(FILE *i);
#endif
>
Instead, one should not rely on the header guards
in the included library and require that the program
that includes foo.h include stdio.h first.
>
Is that true? If so, why?
This is Question 10.7 in the comp.lang.c Frequently
Asked Questions (FAQ) list

Unfortunately, the FAQ doesn't really answer the question
other than to say it's a stylistic question. However, it
did make me realize that it was in the Indian Hill Style
guide that I'd read the opinion that it was a bad idea
to include the necessary headers, as it relies on
the headers being properly guarded. However,
the Indian Hill style guide is now 16 years old...
is there a more definitive answer? (ie, can the FAQ
be updated to say: "Go ahead and include what you
need: current implementations have the proper
header guards in place.")
Frankly, I thought the Indian Hills style guide was pretty bad when it
was new.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 5 '06 #6
On 4 Aug 2006 11:51:43 -0700, "Bill Pursell" <bi**********@gmail.com>
wrote in comp.lang.c:
I read recently (can't remember if it was on this
group or elsewhere) that it is a bad idea to write
a header file this way:

#ifndef FOO_HDR
#define FOO_HDR 1

#include <stdio.h>

int foo(FILE *i);
#endif

Instead, one should not rely on the header guards
in the included library and require that the program
that includes foo.h include stdio.h first.

Is that true? If so, why?
Best practice for whom, and under what circumstances?

We work on embedded systems in C, some of them fairly large for
embedded systems in C, and most of them safety critical. We have
coding rules over which I have the greatest input.

One of the first is that every included file has an include guard.
Period.

The second is that every include file we write must pass the empty.c
test:

#include "include_file_to_test.h"

....must compile without errors or warnings. The only exception is an
include file which contains macros and type definitions only. Then
you might have to add "extern int x;" to empty.c to avoid the
warning/error that some compilers generate because every translation
unit in C is supposed to contain at least one external declaration.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 5 '06 #7
Bill Pursell wrote:
Eric Sosman wrote:
> This is Question 10.7 in the comp.lang.c Frequently
Asked Questions (FAQ) list

Unfortunately, the FAQ doesn't really answer the question
other than to say it's a stylistic question. However, it
did make me realize that it was in the Indian Hill Style
guide that I'd read the opinion that it was a bad idea
to include the necessary headers, as it relies on
the headers being properly guarded. However,
the Indian Hill style guide is now 16 years old...
is there a more definitive answer? (ie, can the FAQ
be updated to say: "Go ahead and include what you
need: current implementations have the proper
header guards in place.")
The standard requires behaviour equivalent to that achieved with header
guards. That is, including a standard header more than once in a
translation unit has no ill effect. (The exception is <assert.hof
course, which can change the behaviour of the assert macro each time it
is included.)

The implementation may not use actual header files but simulate them
internally. Equally, it may not use the #ifndef #define #endif idiom but
some non-standard double-inclusion avoidance feature ('pragma once'
comes to mind).
My choice is to include everything you need. I'd
love to hear the reasons of people who think that's
a bad idea.
That's my choice too.

--
Simon.
Aug 6 '06 #8

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

Similar topics

6
by: JStrummer | last post by:
I have a question regarding paths and the include() statement in PHP. I develop in a Windows environment and will be publishing to a Linux server. I would like to do the following: 1. Setup my...
43
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is...
1
by: Aaron Anodide | last post by:
I ran into a wierd problem today where a case of circular includes caused my build to completely break. All my classes are spit out by a class wizard with "#pragma once" in the header. I...
5
by: Tony Strazzeri | last post by:
Hi all, I a fairly new to html and Javascripting. I have been trying to write some code to hide my email address from spam harvesters. I copied the code from various web examples and modified...
1
by: j.tremlett | last post by:
Hi, I have now drafted a few small-scale schemas and have various types being repeated. I have read that using the 'include' element will allow me to write these once and then call them from...
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
4
by: Ned Balzer | last post by:
Hi all, I am pretty new to asp.net; I've done lots of classic asp, but am just beginning to get my mind wrapped around .net. What I'd like to do is include some code that tests if a user is...
111
by: Nate | last post by:
Hello, I am looking for a method to automatically declare variables in C. I'm not sure if there is a good way to do this, but I had something like this in mind... int i; for(i = 1; i < 4;...
2
by: dave6502 | last post by:
Struggling newbe here, some of my #includes work, some dont. Is it possible to list the include path ? (in BASH), I have looked at the environmental variables (loads of them) but cannot find a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...

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.