1

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?

aaa
  • 13
  • 4
  • 2
    What specific compiler are you using and how did you install it? I think you should show us your full code, the command you are using to compile it, and the exact error message you are getting. ARM toolchains usually come with a C library that provides `sprintf` in my experience. – David Grayson Apr 22 '22 at 00:56
  • @DavidGrayson Using GCC and GDB and installed them through MSYS2 MinGW. I'm building in cygdrive terminal using the latest version of micropython with the command `make BOARD=XX USER_C_MODULES=../../../modules`, [here](https://github.com/micropython/micropython/blob/master/ports/stm32/Makefile) is the makefile. And this is the error message! ```C:\msys64\mingw64\bin\arm-none-eabi-ld.exe: build-KAIROS/kairos_GPSL70/gpsL70.o: in function `gpshide_init': gpsL70.c:(.text.gpshide_init+0x5c): undefined reference to `sprintf' make: *** [Makefile:689: build-KAIROS/firmware.elf] Error 1``` – aaa Apr 22 '22 at 01:13
  • Note: if you know s[n]printf() you don't need strcat(). – wildplasser Apr 22 '22 at 01:39
  • @wildplasser I thought so, but snprintf is not working either – aaa Apr 22 '22 at 01:48

1 Answers1

1

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.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Hello @david! I'm having a similar issue. In the makefile, I've seen that there are two places where they have used -nostdlib. I tried removing that, but it didn't solve my problem. Could you please give us more details regarding that? My task: I'm building a custom Micropython firmware for my ST board using the minimal sample code of Micropython. – Aatif Shaikh Nov 04 '22 at 09:53
  • I think you should post your own question, after carefully reading my very first comment on this question so you know what info is needed. – David Grayson Nov 04 '22 at 15:28