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!
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!
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.
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?
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.
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.
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