我正在尝试使用 NASM 学习汇编,来自 Paul Carter 博士的 pcasm-book.pdf - http://www.drpaulcarter.com/pcasm/ - 在我的 Mac OS X Snow Leopard 上。
我正在尝试将之前编译的 C 示例链接到 asm 示例:
gcc first.o driver.c asm_io.o -o first
但它正在返回它:
driver.c:3: warning: ‘cdecl’ attribute ignored
ld: warning: in first.o, **file is not of required architecture**
ld: warning: in asm_io.o, file is not of required architecture
Undefined symbols:
"_asm_main", referenced from:
_main in ccjLqYJn.o
ld: symbol(s) not found
我使用 Mach-o 格式编译 asm 示例,并且没有错误:
nasm -f macho **first.asm**
nasm -f macho asm_io.asm
如果我尝试在 driver.c 中仅使用 gcc -c,使用 ld 链接所有目标文件,ld 似乎不会链接 driver.o 格式。
ld -o first asm_io.o first.o driver.o
它返回:
ld: warning: in driver.o, file is not of required architecture
Undefined symbols:
"_putchar", referenced from:
print_char in asm_io.o
print_nl in asm_io.o
"_printf", referenced from:
print_int in asm_io.o
print_string in asm_io.o
push_of in asm_io.o
sub_dump_stack in asm_io.o
stack_line_loop in asm_io.o
sub_dump_mem in asm_io.o
mem_outer_loop in asm_io.o
mem_hex_loop in asm_io.o
sub_dump_math in asm_io.o
tag_loop in asm_io.o
print_real in asm_io.o
invalid_st in asm_io.o
"_scanf", referenced from:
read_int in asm_io.o
"_getchar", referenced from:
read_char in asm_io.o
ld: symbol(s) not found for inferred architecture i386
有什么问题吗?在 OS X 上使用 gcc 和 NASM 的正确格式是什么?
谢谢。 丹尼尔·科赫
最佳答案
“文件不符合所需的体系结构”表示您正在尝试链接具有不同体系结构的目标文件:可能是 x86_64 和 i386。由于您的 nasm 输出似乎是 i386,请尝试将 -arch i386
与 gcc 一起使用。您还可以使用 file
显示给定目标文件或库的体系结构。
% touch foo.c ; gcc -c foo.c
% file foo.o
foo.o: Mach-O 64-bit object x86_64
% gcc -c -arch i386 foo.c
% file foo.o
foo.o: Mach-O object i386
关于macos - OSX 中 gcc 使用什么文件格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1912076/