ARM GCC 链接错误 引用未定义函数 _read _write _sbrk 解决和重定向

在使用STM32并遇到使用printf、scanf、malloc等函数时,需要实现_read、_write和_sbrk函数。编译出现错误后,通过查看map文件发现需要重定向这些函数到串口。本文介绍了如何重定向_write和_read到USART以实现PC COM调试打印,并提供了使用serial2实现标准输入输出的示例代码。
AI助手已提取文章相关产品:

使用 printf ,scanf ,malloc 等函数需要实现`_read'`_lseek'`_isatty'`_fstat'`_write'`_sbrk' 函数。

stm32 使用stdlib 时候编译提示错误:

d:/gcc-arm-none-eabi-4_9/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7-m\libc_nano.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
d:/gcc-arm-none-eabi-4_9/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7-m\libc_nano.a(lib_a-writer.o): In function `_write_r':
writer.c:(.text._write_r+0x10): undefined reference to `_write'
d:/gcc-arm-none-eabi-4_9/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7-m\libc_nano.a(lib_a-closer.o): In function `_close_r':
closer.c:(.text._close_r+0xc): undefined reference to `_close'
d:/gcc-arm-none-eabi-4_9/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7-m\libc_nano.a(lib_a-fstatr.o): In function `_fstat_r':
fstatr.c:(.text._fstat_r+0xe): undefined reference to `_fstat'
d:/gcc-arm-none-eabi-4_9/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7-m\libc_nano.a(lib_a-isattyr.o): In function `_isatty_r':
isattyr.c:(.text._isatty_r+0xc): undefined reference to `_isatty'
d:/gcc-arm-none-eabi-4_9/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7-m\libc_nano.a(lib_a-lseekr.o): In function `_lseek_r':
lseekr.c:(.text._lseek_r+0x10): undefined reference to `_lseek'
d:/gcc-arm-none-eabi-4_9/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7-m\libc_nano.a(lib_a-readr.o): In function `_read_r':
readr.c:(.text._read_r+0x10): undefined reference to `_read'

使用 printf ,scanf ,malloc 等函数需要实现`_read'`_lseek'`_isatty'`_fstat'`_write'`_sbrk' 函数。libnosys.a 实现了上述函数 ,可以添加 --specs=nosys.spece来解决这个问题

打开map文件可以看到


当我们需要重定向到usart 到pc com调试打印时候可以重新实现_write() 和 _read() 。

  int _write (int fd, char *pBuffer, int size)
    {
        for (int i = 0; i < size; i++)
        {
            while (!(USART2->SR & USART_SR_TXE))
            {
            }
            USART_SendData(USART2, pBuffer[i]);
        }
        return size;
    }
int _read (int fd, char *pBuffer, int size)
    {
        for (int i = 0; i < size; i++)
        {
            while ((USART2->SR & USART_SR_RXNE) == 0)
            {
            }

            pBuffer[i] = USART_ReceiveData(USART2);
        }
        return size;
    }

重新编译链接,再次打开map文件可以看到



_write() 和 _read()  使用我们实现的函数了,在这里是使用serial2 实现标准输入输出。

运行调试通过call stack 可以看出通过 printf ->_puts_r->__swbuf_r->__sflush_r->_write_r->_write。调用了我们实现的_write 函数了。


下面是示例代码:

#include "stm32f10x_conf.h"
#include <stdio.h>
#include <string.h>
void serial_init()
{
    GPIO_InitTypeDef GPIO_InitStruct;
    USART_InitTypeDef USART_InitStruct;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

    GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
    GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2;
    GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOA,&GPIO_InitStruct);

    GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
    GPIO_InitStruct.GPIO_Pin=GPIO_Pin_3;
    GPIO_Init(GPIOA,&GPIO_InitStruct);

    USART_InitStruct.USART_BaudRate=9600;
    USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
    USART_InitStruct.USART_Parity=USART_Parity_No;
    USART_InitStruct.USART_StopBits=USART_StopBits_1;
    USART_InitStruct.USART_WordLength=USART_WordLength_8b;
    USART_Init(USART2,&USART_InitStruct);
    USART_Cmd(USART2,ENABLE);

}
  int _write (int fd, char *pBuffer, int size)
    {
        for (int i = 0; i < size; i++)
        {
            while (!(USART2->SR & USART_SR_TXE))
            {
            }
            USART_SendData(USART2, pBuffer[i]);
        }
        return size;
    }
int _read (int fd, char *pBuffer, int size)
    {
        for (int i = 0; i < size; i++)
        {
            while ((USART2->SR & USART_SR_RXNE) == 0)
            {
            }

            pBuffer[i] = USART_ReceiveData(USART2);
        }
        return size;
    }
int main(void)
{
    serial_init();
    int in=0;
  while(1)
  {
    printf("hello!\r\n");
    scanf("%d",&in);
    printf("in=%d\r\n",in);

  }
}


您可能感兴趣的与本文相关内容

[ 59%] Linking C executable CTRL_V1 /home/lz/github/stm32/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/14.2.1/../../../../arm-none-eabi/bin/ld: /home/lz/github/stm32/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/14.2.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp/libc_nano.a(libc_a-closer.o): in function `_close_r': closer.c:(.text._close_r+0xc): warning: _close is not implemented and will always fail /home/lz/github/stm32/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/14.2.1/../../../../arm-none-eabi/bin/ld: /home/lz/github/stm32/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/14.2.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp/libc_nano.a(libc_a-closer.o): note: the message above does not take linker garbage collection into account /home/lz/github/stm32/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/14.2.1/../../../../arm-none-eabi/bin/ld: /home/lz/github/stm32/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/14.2.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp/libc_nano.a(libc_a-fstatr.o): in function `_fstat_r': fstatr.c:(.text._fstat_r+0xe): warning: _fstat is not implemented and will always fail /home/lz/github/stm32/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/14.2.1/../../../../arm-none-eabi/bin/ld: /home/lz/github/stm32/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/14.2.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp/libc_nano.a(libc_a-fstatr.o): note: the message above does not take linker garbage collection into account /home/lz/github/stm32/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/14.2.1/../../../../arm-none-eabi/bin/ld: /home/lz/github/stm32/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/14.2.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp/libc_nano.a(libc_a-signalr.o): in function `_getpid_r':
05-16
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值