Discussion:
What is __STDC_HOSTED__ ?
(too old to reply)
James Kuyper
2025-01-22 03:50:38 UTC
Permalink
standard says
"__STDC_HOSTED__ The integer constant 1 if the implementation is a
hosted implementation or the
integer constant 0 if it is not."
What is a hosted implementation?
"A _strictly conforming program_ shall use only those features of the
language and library specified in this document.3) It shall not produce
output dependent on any unspecified, undefined, or
implementation-defined behavior, and shall not exceed any minimum
implementation limit." (4p5)

"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>, <stdbit.h>, <stdbool.h>, <stddef.h>,
<stdint.h>, and <stdnoreturn.h>. Additionally, a conforming freestanding
implementation shall accept any strictly conforming program where:

— the features specified in the header <string.h> are used, except the
following functions:
strdup, strndup, strcoll, strxfrm, strerror; and/or,

— the selected function memalignment from <stdlib.h> is used." (4p6)

Several of the terms in those clauses are in italics, an ISO convention
which indicates that the sentence in which the italicized term occurs
constitutes the official definition of that term. I've indicated where
the italics are by bracketing them with underscores.
Thus, a conforming hosted implementation of C supports all of the
mandatory features of C. A freestanding implementation of C implements
the full C language, but is not required to implement all of the C
standard library. It is only required to implement those parts described
by the specified standard headers, and is not required to implement the
specified functions from the <string.h> and <stdlib.h> headers.
Thiago Adams
2025-01-22 11:29:07 UTC
Permalink
standard says
"__STDC_HOSTED__ The integer constant 1 if the implementation is a
hosted implementation or the
integer constant 0 if it is not."
What is a hosted implementation?
A conforming hosted implementation is one which provides the full
language, or almost the full language except for certain optional
features, such as variable-length arrays in automatic storage.
The opposite of "hosted" is "freestanding". In a freestanding
implementation, large portions of the library may be missing.
The standard specifies exactly which header files and library
features are required in a freestanding implementation,
plus some other details.
For instance, the memory allocation functions like malloc,
and I/O funtions in <stdio.h> are not required in a freestanding
implementation.
"Hosted" referes to the concept that there is a host operating system
platform, whereas "freestanding" refers to the concept of running on the
bare metal, so to speak.
But those are only words; in ISO C they refer to feature sets. A
freestanding implementation doesn't have to be embedded; such a thing
can name sense on a platform that easily supports hosted implementations
(e.g. server or desktop OS), for an application that provides its own
bindings to the operating system: its own memory allocator, I/O streams.
In the GNU environment, we can use "gcc -ffreestanding" to get the
behavior that the __STDC_HOSTED__ macro expands to 0. However, I see
that we can still use <stdio.h> in spite of that, and the library is
linked. "gcc -nostdlib" gets rid of the library linking.
So we have to use both together to get a facsimile of a freestanding
"gcc -nostdlib -ffreestanding -lgcc".
The -lgcc is neeeded because -nostdlib will remove libgcc also, and that
may render the implementation nonconforming. libgcc provides
run-time support for language features such as math operators, which are
not optional in a freestanding implementation.
thanks!
Kaz Kylheku
2025-01-22 02:36:49 UTC
Permalink
standard says
"__STDC_HOSTED__ The integer constant 1 if the implementation is a
hosted implementation or the
integer constant 0 if it is not."
What is a hosted implementation?
A conforming hosted implementation is one which provides the full
language, or almost the full language except for certain optional
features, such as variable-length arrays in automatic storage.

The opposite of "hosted" is "freestanding". In a freestanding
implementation, large portions of the library may be missing.
The standard specifies exactly which header files and library
features are required in a freestanding implementation,
plus some other details.

For instance, the memory allocation functions like malloc,
and I/O funtions in <stdio.h> are not required in a freestanding
implementation.

"Hosted" referes to the concept that there is a host operating system
platform, whereas "freestanding" refers to the concept of running on the
bare metal, so to speak.

But those are only words; in ISO C they refer to feature sets. A
freestanding implementation doesn't have to be embedded; such a thing
can name sense on a platform that easily supports hosted implementations
(e.g. server or desktop OS), for an application that provides its own
bindings to the operating system: its own memory allocator, I/O streams.

In the GNU environment, we can use "gcc -ffreestanding" to get the
behavior that the __STDC_HOSTED__ macro expands to 0. However, I see
that we can still use <stdio.h> in spite of that, and the library is
linked. "gcc -nostdlib" gets rid of the library linking.

So we have to use both together to get a facsimile of a freestanding
implementation which announces itself as such. Perhaps:
"gcc -nostdlib -ffreestanding -lgcc".

The -lgcc is neeeded because -nostdlib will remove libgcc also, and that
may render the implementation nonconforming. libgcc provides
run-time support for language features such as math operators, which are
not optional in a freestanding implementation.
--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @***@mstdn.ca
Loading...