Hi,
I'm using demo code from MCUXpresso SDK 2.9.0 to compile a "bare-metal" program for an ARM Cortex-M33 based microcontroller (specifically, the LPC55S28 but this should be irrelevant to the question).
I added this example code to the main() function (and the relevant include):
double f = 1.0;
printf("%i\n", f);
Compiling this code should give a warning as the format '%i' expects argument of type 'int', but variable f has type 'double'. The warning is based on the compiler option "-Wformat" that is enabled by option "-Wall" (which is enabled by default for this project).
However, no format warning appears.
I wonder how this can be fixed? I find the warning messages very helpful.
I checked the compiler call for compiling this source file and when I remove the option "-specs=redlib.specs" from it, the warning message appears.
Is the "-specs" option required, what does it do and why does it inhibit "-Wformat"?
Thanks.
Dan
Hi @converse , thanks for suggesting to switch to newlib.
To try your suggestion, I switched the project to newlib using "Quicksettings".
Using newlib, the warning message appears as expected.
However, now the compiler emits a warning regarding the following lines:
uint32_t i = 0;
printf("%u\n", i);
The warning reads
warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'uint32_t' {aka 'long unsigned int'} [-Wformat=]
Obviously, with switching to newlib a different stdint.h header file has been included. Using newlib, uint32_t is now defined as long unsigned int.
In comparison, Redlib defines uint32_t as unsigned int.
To fix this, I'll have to adapt all format strings accordingly.
Anything else important that the change to newlib brings along?
Suggest you read this:
https://community.nxp.com/t5/LPCXpresso-IDE-FAQs/What-are-Redlib-and-Newlib/m-p/475288
Basically, redlib is a cut-down C90 library to try to minimise the size of the library (with a few C99 features). Newlib (and it's smaller variant newlib-nano) are based on C99 (and later) so are compliant with more modern C standards.
If you are trying to follow modern standard, you should use newlib.
Dear @converse , of course I've read the page you linked to. While I'm not using a lot of libc functions, I'd like to use and follow a modern C standard. I always use the <stdint.h> and <stdbool.h> headers which are C99 but they are also provided by Redlib.
I'm confused why suddenly gcc warns about the incompatibility of format "%u" and uint32_t type argument.
When I compile the code using, e.g., gcc 9.3.0 (Ubuntu via WSL) I don't get this warning.
Its because there is no guarantee that format specifiers will match the definition across platforms. You will need to use %ul in LPCXpresso and %u on Ubuntu.
The only way to guarantee this is to use the format specifier macros - which looks ugly and unreadable, but avoids errors...
#include <inttypes.h>
uint32_t value;
printf("%"PRIu32"\n", value) ;
See https://en.cppreference.com/w/c/types/integer
and look for "format macro constants"