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

来源:互联网 发布:悟空理财 知乎 编辑:程序博客网 时间:2024/05/01 04:17

使用 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);  }}


0 0
原创粉丝点击