473,770 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2371


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
interdependenci es 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**********@g mail.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.l earn.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**********@g mail.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_t o_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.l earn.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
4667
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 include references in such a way that I don't have to change them all every time I have to publish to the production server 2. Setup above in such a way that won't involve php.ini (& LInux equivalent), as I have access to edit this file...
43
5127
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 relative NOT to the immediate script that is including it, but is relative to the top-level calling script. In practice, this means that you have to constantly worry and adjust paths in includes, based on the startup scripts that call these...
1
2513
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 thought this was equiv with the standard #ifndef scheme. Anyway, instead of figuring out exactly what went wrong (because after an hour all I can come up with is there was a cycle), I decided to use forward declarations, so everywhere I used to have:...
5
1865
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 it to suit me. The code to generate the address is in a js include file. I am using frontpage 2003 to create my web pages. My problem is that the code works OK when I test it using Frontpage's 'Preview' but does not work when I display the page...
1
1245
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 the new schema files and this works fine. However, I was wondering if it is recommended for me to declare elements in this 'common' schema as some of the elements are common also? I ask this because I have read through some newgroups and they
9
4043
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
1841
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 logged in, on each and every page, and redirects the user to a login page if s/he's not logged in. The login page will also take care of some standard setup, such as choosing/populating a user profile. I used to use <!-- #include ... --for this,...
111
4675
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; i++){
2
3792
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 reference. Cheers Dave
0
9617
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
9453
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
10254
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
10099
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
9904
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
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
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
6710
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();...
1
4007
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

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.