?相關問題:
一、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;
}