snprintf compatibility issue between Cross ARM gcc and g++ x64 compiler

Asked by Denis Martins Dantas

Hello,

I'm trying to compile my ARM CM4F firmware to x64 architecture with Cygwin g++. I'm doing this to set up a test environment. I'm having the following issue to port sprintf function:

In my arm firmware I want to use:

char myBuffer[20];
uint32_t myVar = 1234;
uint16_t size;

size = sprintf(myBuffer, "%lu", myVar);

This code compiles with Cross ARM gcc.

When compiling the same code with Cygwin g++, I get the following error:
format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'uint32_t {aka unsigned int}' [-Werror=format=]

I'd like to know if there's any way to port sprintf such way that it compiles for both architectures. Otherwise I will need to adapt my code for when I'm running tests and when I'm compiling my production code, which is not interesting.

Best Regards,

Denis

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
Thomas Preud'homme
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
Best Thomas Preud'homme (thomas-preudhomme) said :
#1

Hi Denis,

You want to use PRIu32, that is sprintf(myBuffer, "%"PRIu32, myVar);

This is described in section 7.8.1 of C99.

Best regards.

Revision history for this message
Denis Martins Dantas (d3nisdantas) said :
#2

Thank you Thomas!! :)

Revision history for this message
Denis Martins Dantas (d3nisdantas) said :
#3

Actually that didn't work with the Cross ARM gcc compiler. I got the error:

format '%u' expects argument of type 'unsigned int', but argument 4 has type 'uint32_t'

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#4

Hi Denis,

What version of GCC ARM embedded are you using? How do you compile it? When compiling:

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>

char myBuffer[20];
uint32_t myVar = 1234;
uint16_t size;

void
foo (void)
{
  size = sprintf(myBuffer, "%"PRIu32, myVar);
}

with "arm-none-eabi-g++ -c -Wall -Wextra -Wformat foo.c" I don't get any error.

Best regards.

Revision history for this message
Denis Martins Dantas (d3nisdantas) said :
#5

I'm not sure about the version I'm using because the my IDE (Kinetis Design Studio) is installed with compilers. However, I'm using GCC compiler - the following command is being issued:

'Invoking: Cross ARM C Compiler'
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Werror -Wunused -Wuninitialized -Wall -Wno-unused-local-typedefs -g -DDEBUG -DTARGET_K22F51212 -DCPU_MK22FN512VLL12 -DUSING_NCN8025_INTERFACE=1 -DTLM9500_APPLICATION -DCLOCK_SETUP=0 -DUSING_FREERTOS -IC:\Freescale\KSDK_1.3.0/platform/osa/inc -IC:\Freescale\KSDK_1.3.0/platform/utilities/inc -IC:\Freescale\KSDK_1.3.0/platform/CMSIS/Include -IC:\Freescale\KSDK_1.3.0/platform/devices -IC:\Freescale\KSDK_1.3.0/platform/devices/MK22F51212/include -IC:\Freescale\KSDK_1.3.0/platform/devices/MK22F51212/startup -IC:\Freescale\KSDK_1.3.0/platform/hal/inc -IC:\Freescale\KSDK_1.3.0/platform/drivers/inc -IC:\Freescale\KSDK_1.3.0/platform/system/inc -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/adc16 -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/cmp -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/crc -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/dac -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/dspi -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/edma -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/ewm -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/flexbus -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/ftm -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/gpio -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/i2c -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/lptmr -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/lpuart -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/pdb -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/pit -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/rnga -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/rtc -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/sai -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/uart -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/vref -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/wdog -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/smartcard -IC:\Freescale\KSDK_1.3.0/platform/drivers/src/smartcard/interface -I"C:\Repositorio_de_Projetos\TLM9500\trunk\sw\tools\kds\linker" -I"C:\Repositorio_de_Projetos\TLM9500\trunk\sw\src" -I"C:\Repositorio_de_Projetos\TLM9500\trunk\sw\src\os" -I"C:\Repositorio_de_Projetos\TLM9500\trunk\sw\src\os\FreeRTOS" -I"C:\Repositorio_de_Projetos\TLM9500\trunk\sw\src\os\FreeRTOS\include" -I"C:\Repositorio_de_Projetos\TLM9500\trunk\sw\src\os\FreeRTOS\portable\GCC\ARM_CM4F" -I"C:\Repositorio_de_Projetos\TLM9500\trunk\sw\src\main" -I"C:\Repositorio_de_Projetos\TLM9500\trunk\sw\src\itf" -I"C:\Repositorio_de_Projetos\TLM9500\trunk\sw\src\app" -I"C:\Repositorio_de_Projetos\TLM9500\trunk\sw\src\util" -I"C:\Repositorio_de_Projetos\TLM9500\trunk\sw\src\drv" -I"C:\Repositorio_de_Projetos\TLM9500\trunk\sw\src\drv\drv_K22F" -std=gnu99 -fno-common -ffreestanding -fno-builtin -mapcs -MMD -MP -MF"src/util/bytes_util.d" -MT"src/util/bytes_util.o" -c -o "src/util/bytes_util.o" "C:/Repositorio_de_Projetos/TLM9500/trunk/sw/src/util/bytes_util.c"

Revision history for this message
Denis Martins Dantas (d3nisdantas) said :
#6

Hello Thomas,

I downloaded the lates GCC ARM embedded toolchain and it did compile now. Thank you very much for your fast replies.

Best Regards,

Denis

Revision history for this message
Denis Martins Dantas (d3nisdantas) said :
#7

Thanks Thomas Preud'homme, that solved my question.