C++: Freestanding Standard Library

https://news.ycombinator.com/rss Hits: 3
Summary

In some earlier articles, we used the term freestanding. We said that a freestanding implementation is one that operates without the support of a hosted operating system. Think embedded systems, OS kernels, or bare-metal environments where heap allocation, system calls, and exception support are typically unavailable. The C++ standard defines a minimal subset of the language and library that must work in such constrained environments.But I wanted to go slightly deeper, because the term doesn’t come up much in everyday C++ — at least not until you start reading the standard more carefully or until you need to use such an implementation.Two kinds of implementationsThis is quite logical. If something is called freestanding, there must be an alternative — the one we’re used to. It does have a name: hosted.To check which one you’re on, inspect the __STDC_HOSTED__ macro. A value of 1 means you’re on a hosted implementation; 0 means freestanding.A few key differences:On a hosted implementation, a program can have more than one thread running concurrently. On a freestanding implementation, this is implementation-defined.On a hosted implementation, a program must contain a global main function. On a freestanding implementation, that requirement is implementation-defined.A hosted implementation must provide all standard headers. For freestanding, even that is implementation-defined — though the standard can mandate that specific entities must be provided or even deleted.What does a program do without main()? Start-up and termination can still be executed, including the constructors and destructors for objects with static storage duration.Note that a vendor is not required to provide a freestanding implementation at all.What is actually guaranteed in freestandingLet’s go beyond “it’s implementation-defined”. The standard does require a small but useful subset of the library to be available in freestanding environments.You can generally rely on low-level, self-contained utiliti...

First seen: 2026-04-10 15:58

Last seen: 2026-04-10 18:00