snprintf()

MartyMacGyver
Posts: 56
Joined: Sun Dec 18, 2016 9:17 pm

snprintf()

Postby MartyMacGyver » Mon Feb 20, 2017 2:06 am

Does snprintf() exist for the ESP32? I see some references to it in esp-idf, but cannot actually seem to use it in code.

(If it just doesn't exist here, I'll work on including my own implementation.)

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: snprintf()

Postby kolban » Mon Feb 20, 2017 2:53 am

A man page on what "snprintf" is can be found here...

https://linux.die.net/man/3/snprintf

Examining the header files for ESP-IDF, it would appear to be included in the distribution. What happens when you try and actually use it in code?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

MartyMacGyver
Posts: 56
Joined: Sun Dec 18, 2016 9:17 pm

Re: snprintf()

Postby MartyMacGyver » Mon Feb 20, 2017 6:53 am

So it's weird... here's the error:

Code: Select all

error: implicit declaration of function 'snprintf' [-Werror=implicit-function-declaration]
note: include '<stdio.h>' or provide a declaration of 'snprintf'
Turns out, if I #include <stdio.h> within the C code file itself, not just in the header file I'm already including, it works. That seems peculiar... if I have a header file with stdio.h in it, and include it, why won't it work? All the other includes work fine.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: snprintf()

Postby ESP_igrr » Mon Feb 20, 2017 6:58 am

snprintf is included, as are most of the STDIO functions (if you find that something is missing, let us know...)

Source code comes from newlib: https://github.com/espressif/newlib-esp32
It's not included within the ESP-IDF repo for two reasons:
- compiling newlib as a component is fairly complex (if you want to achieve zero rebuild behavior on subsequent builds), and newlib build is fairly noisy.
- newlib happens to use certain functions which are embedded into the ESP32 ROM (parts of stdio have been placed into ROM to save space in applications). Therefore arbitrary modifications to newlib source code will not necessarily work (changing some newlib config values will not cause ROM parts of newlib to be updated...).

Newlib source code may be useful while debugging. If this is the case, you can clone the newlib repository mentioned above and add it to your gdb sources path.

MartyMacGyver
Posts: 56
Joined: Sun Dec 18, 2016 9:17 pm

Re: snprintf()

Postby MartyMacGyver » Mon Feb 20, 2017 7:43 am

I figured out what I did wrong, and wouldn't have asked had I not edited the wrong file. :|

Once I straightened that out, I was good to go. Still, it's good to know more about newlib, so there's definitely that!

Best of all - I've released another version of the RGB LED driver! The original code had some serious timing issues that I've resolved - and I understand the RMT module a lot better now too.

https://github.com/MartyMacGyver/ESP32- ... ED-Drivers

Who is online

Users browsing this forum: Baidu [Spider] and 102 guests