Unsigned int is not uint32_t when compiled to cortex-m0 - possible C compiler flag error
I needed to port a project to run from Eclipse with its own Makefile. I changed his makefile and I think the error is related to it or the compiler flag.
Host: Virtualbox Win 8, x64, target device: nrf51822, which is the cortex-m0. I am using gnu arm cross compiler 4.8.4 (GNU Tools ARM Embedded)
Compilation displays the following error / warning message:
src/main.c:173:4: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' [-Werror=format=]
I do not understand. uint32_t is unsigned int . I have included stdint.h.
I am compiling sources with the following flags:
CFLAGS += -mcpu=cortex-m0 -mthumb -mabi=aapcs --std=gnu11 -funsigned-char -DNRF51 -DDEBUG_NRF_USER -DBLE_STACK_SUPPORT_REQD -DBOARD_PCA10000 -DNRF51822_QFAA_CA CFLAGS += -Wall -Werror CFLAGS += -mfloat-abi=soft
Doesn't -mcpu = cortex-m0 give the size of an integer? The pre-processor macros stdint.h should generate "typedef unsigned int __uint32_t;". Eclipse shows that this line has been compiled, but I don't know if I can trust it because the external makefile is used with its own compiler.
source to share
uint32_t
is a typedef (alias) for some predefined unsigned integer type. This type is guaranteed to be exactly 32 bits wide, with no padding bits. You cannot safely assume that this is an alias for any particular type. It could probably be either unsigned int
or unsigned long int
. (Less plausible, it could be unsigned char
either unsigned short
on an unusual system, or it could be an extended integer type, it can't be unsigned long long
at least 64 bits wide.)
Printing a value uint32_t
using is "%u"
not portable. You can get away with it if your implementation happens to determine uint32_t
how unsigned int
(apparently not). You can possibly get away from it if unsigned int
- 32 bits.
The correct format for uint32_t
is defined as a macro in <inttypes.h>
:
uint32_t x = 42;
printf("x = %" PRIu32 "\n", x);
( PRIu32
expands to a string literal, this takes advantage of the fact that adjacent string literals are concatenated.)
An easier way is to convert the value to a known type:
uint32_t x = 42;
printf("x = %ju\n", (intmax_t)x);
or perhaps:
uint32_t x = 42;
pritnf("x = %llu\n", (unsigned long long)x);
source to share