STM32
直播中

刘强

7年用户 1180经验值
私信 关注
[问答]

怎样去解决使用printf进行打印会报错找不到FILE的问题呢

怎样去解决使用printf进行打印会报错找不到FILE的问题呢?

回帖(1)

马健

2021-12-2 10:55:40
1、配置好串口相关参数
2、可新建一个usart.c,或将代码存放到其他位置
3、添加如下代码

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
  return ch;
}
即可使用printf进行打印..编译时可能会报错找不到FILE,可将FILE进行重定义
typedef struct __FILE FILE;
举报

更多回帖

发帖
×
20
完善资料,
赚取积分