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
- ARM GCC 链接错误 引用未定义函数 _read _write _sbrk 解决和重定向
- c/c++ _read() 和 _write() 和 _lseeki64() 和 read() 和 read() 函数
- openjpeg:解决静态链接时未定义引用错误:undefined reference to `__imp_opj_xxxxxxx'
- 用gcc-none-arm 编译出现undefined reference to `_sbrk'
- C++链接错误“对函数或变量未定义的引用”解决办法
- 非法链接和重定向
- windows命令行输出重定向和错误重定向
- windows命令行输出重定向和错误重定向
- php header()函数重定向错误
- php header()函数循环重定向错误
- 解决未定义类型错误
- 函数未定义错误排查
- 解决 Qt5 使用 OpenGL 函数未定义的错误
- gcc 编译器对 sqrt 未定义的引用
- gcc 编译器对 sqrt 未定义的引用
- gcc 编译器对 sqrt 未定义的引用
- gcc 对‘sqrt’未定义的引用
- 重定向编程 dup和dup2函数
- HTML+CSS基础 i标签使字体变成斜体
- SaaS服务和相关概念
- |洛谷|数学|P1147 连续自然数和
- 计算机图形学 第一章 图形系统和模型
- javaBean之四个作用域范围
- ARM GCC 链接错误 引用未定义函数 _read _write _sbrk 解决和重定向
- Java不指定长度的二维数组
- 网教28. 递归
- angularJS 学习记录
- 问题锦集
- ACM简单题目中对于连续多组输入的处理方法
- 如何将uboot里面的参数传给recovery或kernel
- HDU 2063 Investment(多次完全背包)
- UVA 1572 Self-Assembly