cstdiomicropythonusermode

sprintf is undefined for STM32


I'm making a cusermod for Micropython, and I'm trying to store a number in hex format in a string. I tried using sprintf however, my compiler is telling me that it's undefined. I imported stdio.h, string.h as well as some other libraries required to develop cusermods for STM32 chips, yet I van't figure out why sprintf isn't defined??? I've only had this issue when using format specifiers...

Here's my code!

int chks = checksum(mns);

char checksum[10] = "*";
char temp[6]; 
sprintf(temp, "%x\r\n",chks);
strcat(checksum,temp);

char str[23] = "$";
strcat(str,mns);
strcat(str,checksum);
strcpy(mns,str);

I've used printf and it works fine even when I use format specifiers. Has anyone had a similar issue or any idea how to solve it?


Solution

  • In two places, your Makefile has the -nostdlib flag, which tells the linker to not link in the C standard library, which provides things like sprintf.

    In the future, you can catch problems like this yourself by preparing a minimal reproducible example, including a minimal command for compiling the example. Since the compilation command would be minimal, you would easily be able to see suspicious options like -nostdlib and you would try removing such options to make the example more minimal.