IAR9.X printf串口重定向方法,或提示Linker Error: “no definition for __write“的解决方案

一、问题现象:

1、Error[Li005]:no definition for"__write" [referenced from flush.o(dl7M_tlf.a)] 

2、串口重映射代码没问题,但是串口工具接收不到数据

3、复现环境:IAR9.40.1

二、操作方法:

1、[工程项目]->[Options]->[General Options]->[Library Configuration]配置如下

2、在串口初始化页面新增如下代码

3、源码如下

#include <LowLevelIOInterface.h>
int iar9x_putc(int x);
#pragma module_name = "?__write"


#ifdef __GNUC__
/* With GCC, 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 iar9x_putc(int ch)
#endif /* __GNUC__ */


/*
* If the __write implementation uses internal buffering, uncomment
* the following line to ensure that we are called with "buffer" as 0
* (i.e. flush) when the application terminates.
*/
size_t __write(int handle, const unsigned char * buffer, size_t size)
{
    size_t nChars = 0;
    if (buffer == 0)
    {
        /* This means that we should flush internal buffers. Since we don't we just return
        * (Remember, "handle" == -1 means that all handles should be flushed.) */
        return 0;
    }
    /* Only writes to "standard out" and "standard err", return failure for other handles*/
    if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
    {
        return _LLIO_ERROR;
    }
    for (/* Empty */; size != 0; --size)
    {
        if (iar9x_putc(*buffer++) < 0)
        {
            return _LLIO_ERROR;
        }
        ++nChars;
    }
    return nChars;
}


/*** @brief Retargets the C library printf function to the USART. ***/
PUTCHAR_PROTOTYPE
{
 /* Place your implementation of fputc here */
 /* e.g. write a character to the USART and Loop until the end of transmission */
 HAL_UART_Transmit(&husart_debug, (uint8_t *)&ch, 1, 0xFFFF);
 return ch;
}

三、原理分析

1、IAR 帮助文档“EWARM 开发指南”,可以通过 Help-> C/C++ Development Guide 查看 EWARM_DevelopmentGuide.ENU.pdf 文件

2、文档的 The DLIB runtime environment 中“BRIEFLY ABOUT RETARGETING”章 节有重定向的较详细说明,如下图 2所示。IAR9.x DLib 使用 printf 时需要重定向__write 函数,详见文档

3、在 \arm\src\lib\file\write.c 文件已经给出了一个__write 的实现模 板,客户可以自行参考。

四、参考链接:

【应用笔记】LAT1295 IAR 9.x环境下STM32 printf 重定向串口输出 - STM32团队 ST意法半导体中文论坛

IAR9.X printf串口底层重定向方法,否则提示Linker Error: "no definition for __write" - 开发环境 - 硬汉嵌入式论坛 - Powered by Discuz!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值