cancel
Showing results for 
Search instead for 
Did you mean: 

Printf Retarget only works with syscalls.c

lgacnik97
Associate III

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.

1 ACCEPTED SOLUTION
3 REPLIES 3
MM..1
Chief II

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

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?