← Back to context

Comment by einpoklum

2 months ago

While "newlib" is an interesting idea - the approach taken here is, in many cases, the wrong one.

You see, actually, the printf() family of functions don't actually require _any_ metal, bare or otherwise, beyond the ability to print individual characters.

For this reason, a popular approach for the case of not having a full-fledged standard library is to have a fully cross-platform implementation of the family which "exposes" a symbol dependency on a character printing function, e.g.:

  void putchar_(char c);

and variants of the printf functions which take the character-printing function as a runtime parameter:

  int fctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, ...);
  int vfctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, va_list arg);

this is the approach taken in the standalone printf implementation I maintain, originally by Marco Paland:

https://github.com/eyalroz/printf