2

Is there some clear-cut distinction between standard C *.h header files that are provided by the C compiler, as oppossed to those which are provided by a standard C library? Is there some list, or some standard locations?

Motivation: int this answer I got a while ago, regarding a missing unistd.h in the latest TinyC compiler, the author argued that unistd.h (contrarily to sys/unistd.h) should not be provided by the compiler but by your C library.

I could not make much sense of that response (for one thing shouldn't that also apply to, say, stdio.h?) but I'm still wondering about it. Is that correct? Where is some authoritative reference for this?

Looking in other compilers, I see that other "self contained" POSIX C compilers that are hosted in Windows (like the GCC toolchain that comes with MinGW, in several incarnations; or Digital Mars compiler), include all header files.

And in a standard Linux distribution (say, Centos 5.10) I see that the gcc package provides a few header files (eg, stdbool.h, syslimits.h) in /usr/lib/gcc/i386-redhat-linux/4.1.1/include/, and the glibc-headers package provides the majority of the headers in /usr/include/ (including stdio.h, /usr/include/unistd.h and /usr/include/sys/unistd.h).

So, in neither case I see support for the above claim.

leonbloy
  • 73,180
  • 20
  • 142
  • 190

2 Answers2

3

No, there is no clear-cut distinction.

As far as the C standard is concerned (here's a recent draft), the compiler and the library together make up the implementation, distinguished mostly by being described in sections 6 and 7 of the standard, respectively.

For some implementations, the compiler and the runtime library are provided by the same vendor/organization/person, either as a single installable package or as two separate packages. For other implementations (including gcc), the bulk of the standard library is provided by the underlying operating system, but the installation package for the compiler includes a few of its own headers.

Another example: When you install gcc from source on Solaris, the installer runs a script that grabs copies of some of the existing header files (provided by Sun's Oracle's runtime library) and edits them, installing the modified copies in a separate directory.

On GNU/Linux systems, the default C compiler is usually gcc, and the runtime library is provided by glibc -- both GNU packages, but developed separately. The MinGW implementation under Windows uses the gcc compiler with Microsoft's runtime library (which leads to some problems because they disagree on the representation of long double).

The choice of which standard headers need to be provided by the compiler is made by the authors of the compiler. Headers whose implementation is tightly tied to a particular compiler (such as <stdint.h>, <limits.h>, and <float.h>) are typically provided by the compiler; headers that provide an interface to operating system services, like <stdio.h> and <stdlib.h> are typically provided by the runtime library or perhaps by the OS.

The C standard provides no direct guidance regarding how this choice should be made.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Thanks. Do you have any opinion about the linked affirmation, about the distinction `unistd.h` vs `sys/unistd.h`? – leonbloy Jun 12 '14 at 15:58
0

Except for embedded (free-standing) C implementations, it makes little sense to separate C into compiler and libraries. Neither a compiler without libraries, not a C library without a compiler make much sense. Only a compiler together with a library makes up a complete implementation.

Since C89, the standard library is part of the C standard, and the list of required header files is listed in the standard. Other sets of libraries are standardized by Posix, X/Open ... See this answer for a list: List of standard header files in C and C++

There are some headers that are by nature closer to the compiler itself, e.g. limits.h, which specifies the size of the data types. Some headers are closer to the OS, i.e. unistd.h. In both cases however, there are intersections, and if the OS' idea of, say, size_t and the compiler's implementation do not agree, nothing will work.

One could also argue that unistd.h should be provided by the OS and not by the C library - after all, the knowledge how to call a kernel function belongs into the OS.

In summary, I think this distinction into compiler, C library, operating system makes little sense.

Community
  • 1
  • 1
Chris
  • 4,133
  • 30
  • 38
  • 'Neither a compiler without libraries […] make much sense.' There are free-standing implementations, of course it makes sense. – mafso Jun 12 '14 at 15:29
  • @mafso I'm aware of that, but I think the question author had hosted implementations in mind. In free-standing, the compiler basically contains its own library, which is IMO also a point that the separation argument is misguided. – Chris Jun 12 '14 at 15:33
  • OP was explicitly asking for an implementation without the standard library, which _is_ a free-standing one. The question was, if `` has to be part of such a free-standing implementation and C11 §6.1 requires only the headers ``, ``, ``, ``, ``, ``, ``, ``, `` for such an implementation. – mafso Jun 12 '14 at 15:45
  • @mafso: The OP said nothing about freestanding implementations. For many *hosted* implementations, the compiler and the library (which together make up the "implementation") happen to be provided separately. On most Linux systems, the C implementation consists of gcc and glibc. On Solaris, for example, it might consist of gcc and the Solaris library. Which means I disagree with Chris's answer; the distinction matters a great deal, because a C implementation can be assembled from different components. – Keith Thompson Jun 13 '14 at 15:13
  • "*There are some headers (or "facilities" in standard parlance)*" -- The C standard does not use the word "facilities" in this sense. – Keith Thompson Jun 13 '14 at 15:15
  • @KeithThompson thanks - got that from Harbison & Steele and did not verify in Standard. Edited answer. – Chris Jun 13 '14 at 15:23