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

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

  }
}


  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
gcc 中 __attribute__ 是用来声明特殊属性的关键字,常用于指定变量、函数、类型等的特定属性。以下是 __attribute__ 的常用参数、说明和举例: 1. aligned(n) 说明:指定变量的对齐方式,n 为变量的对齐值,必须为 2 的幂次方。 举例:__attribute__((aligned(16))) int a; 2. packed 说明:指定变量或结构体不进行字节对齐,节省内存空间。 举例:__attribute__((packed)) struct myStruct { char a; int b; }; 3. deprecated 说明:标记函数或变量已过时,编译器会给出警告信息。 举例:__attribute__((deprecated)) void old_function(); 4. noreturn 说明:标记函数不会返回,可以用于告诉编译器代码不会继续执行下去。 举例:__attribute__((noreturn)) void error_exit(); 5. format(fmt, arg) 说明:指定函数的参数格式和类型,类似于 printf 函数。 举例:__attribute__((format(printf, 1, 2))) void my_printf(const char *format, ...); 6. unused 说明:标记变量或函数被使用,编译器会给出警告信息。 举例:__attribute__((unused)) int unused_variable; 7. constructor(priority) 说明:标记函数为构造函数,可以指定优先级,指定的函数会在 main 函数之前自动执行。 举例:__attribute__((constructor(101))) void my_constructor(); 8. destructor(priority) 说明:标记函数为析构函数,可以指定优先级,指定的函数会在 main 函数之后自动执行。 举例:__attribute__((destructor(101))) void my_destructor();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值