Both of the following statements work on an Arduino Uno WiFi Rev2 board but the second statement does not work on the new Arduino Uno R4 WiFi board.
Serial.print("strlen = "); Serial.println(strlen(string));
Serial.print("strnlen = "); Serial.println(strnlen(string, 10)); // error: 'strnlen' was not declared in this scope
The strlen() function is defined, but the strnlen() function is not defined. Anybody know why this is the case and perhaps how to fix it?
There are a few functions missing from the standard C library included with this core. Another is strdup(). They may be optional according to the standards documentation.
I created a strnlen() macro that works within a sketch, but the problem I am having is that the strnlen() function is used within a library, specifically the PubSubClient library. I already submitted an issue for that library, but I thought it would be wise to also acknowledge that lack of the strnlen() function here and see if anybody knew of a work-a-around.
You are welcome to make a copy of the library, and fix that problem for yourself. The issue you posted on the Github site will make others aware of the problem.
They are also both present in AVR libc, used by the original Uno R3 core. Users, and library authors, will have become used to their availability. I guess that makes them de facto standards.
Alas, tracing why __POSIX_VISIBLE gets set to a particular value seems to be a non-trivial task (or at least requires tool knowledge that I don't have.)
SAMD compiles, which theoretically use the same toolchain, work fine.
(and indeed, R4 compiles have #define __POSIX_VISIBLE 199209, while SAMD has 200809)
Ahh. A bit of brute force says that __POSIX_VISIBLE gets set based on _POSIX_C_SOURCE, which defaults to 200809 unless _XOPEN_SOURCE is set less than 700.
For some reason, the R4 build line includes a -D_XOPEN_SOURCE (SAMD doesn't define it at all.)
I don't know if that's intentional, or if someone thought that it means "Open Source" in the sense of OSSW. (I dont think that that's what it means!) (I can't find anything in the Arduino R4 core, the Arduino-api core, or the Renesas fsp code, that actually references _XOPEN_SOURCE
This does imply that the problem is in the #include files, and the function is actually present in the C library that is linked in. So a workaround is simply to provide an explicit prototype like:
Great analysis! I tried now to add the prototype and it works.
Even more amazing, if you leave away _XOPEN_SOURCE in platform.txt then the original PubSubClient complies without the prototype.
What is broken then is the buildin RTC. It doesn't compile any more because it redefines the struct timeval. After removing this in RTC.h everything works fine.
I compiled with RTC and WiFiS3 now and they seem to work. Can be that some of the other libraries cause problems because they also redefine structures. Will check this a little more over the next couple of days. If there is no big thing coming, then the obvious solution would be a new platform.txt file in the next version of the code.