I read this official manual about GCC. Sometimes I have a a problem with translating of text. On the page number six (chapter 2.1) I can't understand such fragment of text:
The ISO C standard defines (in clause 4) two classes of conforming implementation. A conforming hosted implementation supports the whole standard including all the library facilities; a conforming freestanding implementation is only required to provide certain library facilities: those in
<float.h>
,<limits.h>
,<stdarg.h>
, and<stddef.h>
; since AMD1, also those in<iso646.h>
; since C99, also those in<stdbool.h>
and<stdint.h>
; and since C11, also those in<stdalign.h>
and . In addition, complex types, added in C99, are not required for freestanding implementations. The standard also defines two environments for programs, a freestanding environment, required of all implementations and which may not have library facilities beyond those required of freestanding implementations, where the handling of program startup and termination are implementation-defined, and a hosted environment, which is not required, in which all the library facilities are provided and startup is through a functionint main (void)
orint main (int, char *[])
. An OS kernel would be a freestanding environment; a program using the facilities of an operating system would normally be in a hosted implementation.
I am not sure I understand it right...
I will rephrase how I understood it:
The light versions is for OS developing. The full versions is for programs, which will work in OS.
And I not understood the phrase about the main
function.
I ask to explain me this fragment of text.
It's a bit of both.
The standard defines two runtime environments. One has all of the language, plus a tiny subset of the standard runtime library, plus additional implementation-defined stuff. That's a freestanding environment, and is (as you guessed) intended for programming on the bare metal, e.g. an OS kernel.
The other, more sophisticated environment includes all of the facilities of the above plus all of the standard runtime library. That's a hosted environment, which is intended for application programming.
Now, an implementation is only required to include the facilities of the freestanding environment. If that's all it has, it's called a freestanding implementation. Cross-compilers for deeply embedded microcontrollers are often freestanding implementations, because much of the standard C runtime doesn't make sense or is too big to fit.
Implementing the hosted environment is optional; if an implementation provides the hosted environment it's called a hosted implementation. A hosted implementation must also provide the freestanding environment, i.e. a compilation mode in which only the facilities of a freestanding implementation are available. (This mode would typically be used for compiling things like the C runtime itself, most of which is just more C.)
Finally, the standard signatures for main
(int main(void)
and int main(int, char **)
) are part of the hosted environment. A freestanding environment can use those signatures as well, but it can also define the signature of main
to be whatever it likes (void main(void)
is common) or use a different name for the entry point.
C11 4/6:
The two forms of conforming implementation are hosted and freestanding. A conforming hosted implementation shall accept any strictly conforming program. A conforming freestanding implementation shall accept any strictly conforming program in which the use of the features specified in the library clause (clause 7) is confined to the contents of the standard headers
<float.h>, <iso646.h>, <limits.h>, <stdalign.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, <stdint.h>, and <stdnoreturn.h>
.
First note that "implementation", in the context of the C standard, means "the implementation of a C compiler" and nothing else.
As you correctly state in your question, the freestanding implementation is a compiler for a system that isn't intended to have an operative system beneath it. In other words a freestanding implementation compiler produces programs that are either embedded applications running on a "bare bone" CPU, or programs that are operative systems in themselves. While a hosted implementation is a compiler intended for applications running on top of a OS.
The compiler for freestanding applications only needs to provide the above mentioned headers. The rest of the headers (such as stdio.h) are defined in the mentioned "clause 7" of the standard, but they are not mandatory for a freestanding implementation.
Note however that several libraries are not mandatory to neither hosted nor freestanding implementations, for example the complex number library: C11 7.3.1:
"Implementations that define the macro _ _STDC_NO_COMPLEX_ _ need not provide this header nor support any of its facilities."
Furthermore, the two different execution environments, freestanding and hosted, allow different syntax for main(), more info can be found here. A very common misunderstanding among programmers is that the only allowed form in C is int main()
, which is only true in a hosted environment.
For example, a freestanding program could start at an out-of-reset interrupt service routine. From there it can call a void main()
function, or it could call some other function entirely: it is implementation-defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With