views:

324

answers:

5

Is there a guarantee that each library header looks something like that?

#ifndef STDIO_H
#define STDIO_H
/* contents here... */
#endif

Can you please refer me to a source?

Thanks!

+6  A: 

No, GCC does not protect you from a library not using include guards like you described - that is up to the library in question. (And not part of GCC.)

All prominent C standard libraries (glibc, newlibc, ulibc) do guard their includes correctly. (As they are widely used, such a blatant problem would be quickly discovered.)

Edit: After your second comment, your question makes more sense now. Quoting from ISO/IEC 9899:1999 (C99), chapter 7.1.2 Standard Headers, paragraph 4, first sentence:

Standard headers may be included in any order; each may be included more than once in a given scope, with no effect different from being included only once, except that the effect of including <assert.h> depends on the definition of NDEBUG (see 7.2).

That means, if any Standard C library you come across gives you trouble, it's broken.

DevSolar
C standard library.
Leif Ericson
Well, *which* one? ;-) While not relevant to your answer (since none of the better-known C libraries around will make any problems in this area), you asked for reference to "a source", and that could only be the source of a *specific* C standard library. You know, while there is a standard those libraries must adhere to (ISO/IEC 9989), there are multiple *implementations* of that standard around...
DevSolar
I know. What I wanted to know is what the standard said about that. By which I mean ANSI C. It was a mistake talking about GCC, I should have said ANSI C instead.
Leif Ericson
See edited answer.
DevSolar
+3  A: 

The C++ Standard certainly specifies that standard library headers can be #included more than once. It doesn't specify what the mechanism to avoid multiple definitions must be though. I excpect the C standard (which I have not got) says something similar. But why are you concerned about this?

anon
Because I have the files `pass1.h' and `pass1.c' and in pass1.c there is #include <stdio.h> and also in pass1.h.I know I can just remove #include <stdio.h> from pass1.c because it is already included in pass1.h but I wondered if something can break if I don't.
Leif Ericson
No, nothing will break - you can include the file as often as you like.
anon
A: 

There are autoconf scripts written to introduce a protection against multiple inclusion of header files for source bases. I am sure this would be done for the Standard C Library.

nik
A: 

Here is the most relevant section I could find in the most recent draft:

6.10.2 6 - A #include preprocessing directive may appear in a source file that has been read because of a #include directive in another file, up to an implementation-defined nesting limit (see 5.2.4.1).

Section 5.2.4.1 then enumerates the minimum required values for various environmental limits; for nested file inclusion, it's 15.

How this is accomplished is left up to the individual implementation; the standard does not mandate the use of #include guards, although it's reasonable to assume that's how most implementations do it.

John Bode
that is not a relevant section at all. That has to do with *nested* includes, *not* redundant includes.
+5  A: 

The C99 standard (ISO/IEC 9899:TC3) specifically states:

Standard headers may be included in any order; each may be included more than once in a given scope, with no effect different from being included only once, except that the effect of including <assert.h> depends on the definition of NDEBUG (see 7.2).

Point 4 in section 7.1.2

Evan Teran
+1 for siting relevant part of the standard.