I am trying to retarget printf()
function for STM32F411RET microcontroller in ARM GCC toolchain environment which uses Newlib for standard C library.
When I search for how to retarget printf()
, many people says I need to implement _write()
or _write_r()
. And it seems both working.
But I still have questions about them:
When I look through the document of Newlib, it says I can implement
write()
to output files, but it doesn't look working. It looks like we can implement_write()
but this function never be mentioned in the document. What happend towrite()
? does an underscore make anything different?In which situation
_write_r()
is preferable than_wirte()
? I don't understand the concept of reenterncy in C. Any examples?
Thanks for reading this.
write
function, conforming programs are allowed to define their own function (or variable) namedwrite
without it changing howprintf
behaves. This means thatprintf
can't use a function namedwrite
, it has to use a function with a name that conforming programs aren't allowed to use, like say_write
. – Alisawrite()
will work withopen()
; they are standard POSIX type functions. The special files number 0, 1, 2 are for stdin, stdout, stderr (typically). You write the implementation of these for newlib and if the file is >2 then it is some special file which you must index somehow. Ie, theopen()
would have specified a desired file to write to and returned a file number >2. USER code must not rely on these values. However, implementers need to define something. – Rocailleprintf
toputs
and in the process break everything. In this case you can either implementputs
too or rename printf to something else. – Caniff