【发布时间】:2021-09-07 02:26:54
【问题描述】:
在我的嵌入式项目中,我收到了 printf 语句所需的未定义的 _write 引用。使用 arm-none-eabi-gcc 9.3.1。
uart.c实现_write,如下图:
int _write(int file, char* ptr, int len) __atribute__ ((used));
int _write(int file, char* ptr, int len)
{
...
}
void foo()
{
nop();
}
uart.c 被编译成静态库,在编译可执行文件时链接。
这里是 main.c:
int main()
{
//foo();
printf("Hello world!");
}
注释掉foo();,我得到undefined reference to '_write'。取消注释 foo(); 后,项目将按预期构建。如何在不包含虚拟函数的情况下构建它?
构建步骤:
arm-none-eabi-gcc -c uart.c
ar rvs uart.a uart.o
arm-none-eabi-gcc main.c uart.a
【问题讨论】:
-
@Someprogrammerdude 不,在这种情况下不是。库 grepping write 的 Objdump 返回:
00000000 g F .text._write 00000018 _write -
uart.c is compiled into a static library, which is linked while compiling the executable.请显示编译命令。参数的顺序是什么? -
@KamilCuk 我用我的构建步骤更新了问题
-
能否请您逐字发布您收到的所有错误消息?你没有得到
undefined reference to _lseekundefined reference to _exit等吗?也请s/__atribute__/__attribute__。 -
@KamilCuk 我发布的文件只是我的大型 cmake 项目的简化。我在 cmake 项目中遇到的唯一错误是:
writer.c:(.text._write_r+0x10): undefined reference to '_write'。是的,我也得到了对其他人的未定义引用,但在我的 cmake 项目中我没有,因为它们是在正确链接的其他文件中定义的。