【发布时间】:2015-07-07 18:52:53
【问题描述】:
我有一个非常简单的main.c 文件:
#include <stdio.h>
int cnt;
extern void increment();
int main()
{
cnt = 0;
increment();
printf("%d\n", cnt);
return 0;
}
还有更简单的hello.asm:
EXTERN cnt
section .text
global increment
increment:
inc dword [cnt]
ret
首先我通过输入gcc -c main.c 得到main.o
然后我得到hello.o -- nasm -f macho hello.asm -DDARWIN
最后,为了得到一个可执行文件,我执行ld -o main main.o hello.o -arch i386 -lc 并得到一个错误:
ld: warning: -macosx_version_min not specified, assuming 10.10
ld: warning:
ignoring file main.o, file was built for unsupported file format ( 0xCF 0xFA 0xED 0xFE 0x07 0x00 0x00 0x01 0x03 0x00 0x00 0x00 0x01 0x00 0x00 0x00 ) which is not the architecture being linked (i386): main.o
Undefined symbols for architecture i386:
"_main", referenced from:
implicit entry/start for main executable
"cnt", referenced from:
increment in hello.o
ld: symbol(s) not found for architecture i386
如何解决此链接错误?
【问题讨论】:
-
@EOF 因为那不起作用?
gcc不知道调用nasm -
@Jester:是的,我刚刚意识到它不是用气体组件写的......
-
您使用的是 64 位操作系统吗?然后为您的
main.c执行gcc -m32。根据@EOF 的建议,您也可以在获得hello.o后使用gcc:gcc main.c hello.o(如有必要,添加-m32)。 -
@Jester with -m32 option I don't get file was built for unsupported file format error。但其他错误仍然存在。
-
这可能有效,尝试在程序集文件中定义
cnt像这样的数据部分cnt dw 0并通过在其上方添加global cnt使其像increment一样全局并删除 @ 987654342@线。现在在c源代码中将int cnt更改为extern int cnt,希望这应该让所有内容都能正确链接。