warning: _close is not implemented and will always fail
相关问题:
一、undefined reference to `_exit
undefined reference to ‘end‘
warning: _close is not implemented and will always fail
一、环境:
ubuntu24.04实体机、
arm-none-eabi-gcc gcc version 13.2.1 20231009 (15:13.2.rel1-2)
二、问题:
相同的错误还有:
warning: _close is not implemented and will always fail
warning: _lseek is not implemented and will always fail
warning: _read is not implemented and will always fail
warning: _write is not implemented and will always fail
三、分析
是因为libnosys.a
,它提供了一个“空壳”实现(stub)来防止链接错误,但所有 I/O 系统调用(如 _close
, _read
, _write
, _lseek
, _open
, _fstat
, _isatty
等)都只是返回错误码 -1
并设置 errno = ENOSYS
四、解决
你只是裸机跑程序,不使用文件系统、串口 I/O | ✅ 可以忽略 |
你用了 printf/scanf ,但重定向到了 UART | ✅ 可以忽略 |
你用了 fopen/fclose 或 POSIX 文件操作 | ❌ 会失败,需要实现系统调用 |
✅ 方法一:忽略警告(推荐裸机开发)
在链接时添加:
bash
复制
-Wl,--allow-multiple-definition -lnosys
✅ 方法二:自己实现 _close
(如果你需要)
如果你确实用了 close()
,可以手动实现一个空壳版本:
c
复制
#include <errno.h>
#include <unistd.h>int _close(int file) {errno = ENOSYS;return -1;
}
✅ 用自己的函数实现裸机开发(_close
, _read
, _write
, _lseek
, _open
)
如果你只是用 printf
重定向到 UART,只需要实现 _write
,其他的系统调用可以保留为 stub:
c
int _write(int file, char *ptr, int len) {// 重定向到 UARTfor (int i = 0; i < len; i++) {uart_send(*ptr++);}return len;
}