Skip to main content
Graduate II
July 8, 2023
Solved

Printf Retarget only works with syscalls.c

  • July 8, 2023
  • 1 reply
  • 3494 views

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. 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.

    This topic has been closed for replies.

    1 reply

    Graduate II
    July 8, 2023

    Show how you implement it in main.cpp. I mean you omit extern C

    lgacnik97Author
    Graduate II
    July 8, 2023

    Like this:

    int __io_putchar(int ch);
    
    int __io_putchar(int ch)
    {
     HAL_UART_Transmit(UART_GetHuart3(), (uint8_t *)&ch, 1, 0xFFFF);
     return ch;
    }

    With extern C, you are referring to C and C++ header guards?

    MM..1Answer
    Graduate II
    July 8, 2023