I'm using STM32F7xx for my project where I use VS Code text editor in combination with CMake project build. I also use CubeMX for code generation. For a record I have already examined all the answers in this question but none seems to work.
When I generate code using STM32CubeIDE template, the syscalls.c file is generated. It provides the following declaration:
extern int __io_putchar(int ch) __attribute__((weak));
If I provide __io_putchar() definition in my main.cpp file nothing happens after using printf(). However, if I provide __io_putchar() function definition inside of syscalls.c then printf() operates as should and I get output stream printed out on terminal.
extern int __io_putchar(int ch) __attribute__((weak));
int __io_putchar(int ch)
{
HAL_UART_Transmit(UART_GetHuart3(), (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
I have tried several other methods of printf() retarget, like retargeting _write or fputc, however nothing really works besides providing function definition inside of syscalls.c. And the problem of providing __io_putchar() function definition inside of syscalls.c is that after re-generating code from CubeMX, this definition will be removed. Therefore I need more "clean" solution for this.
__attribute__((weak))should be applied to the function definition. I don't think putting it on the prototype does anything useful. But since it's auto-generated by CubeMX even if you fix it it'll get overwritten then next time you regenerate the project. – brhans Jul 09 '23 at 00:13__attribute__((weak))in your main.cpp? If you are then that could be what's causing the trouble. – brhans Jul 09 '23 at 00:17extern "C"syntax since I was using a C function in C++ file. – lucenzo97 Jul 13 '23 at 20:47