4

我正在尝试在我的 Mac OS X Snow Leopard 上使用 NASM(Paul Carter 博士的 pcasm-book.pdf - http://www.drpaulcarter.com/pcasm/ )来学习组装。

我正在尝试将之前编译的 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 的正确格式是什么?

谢谢你。丹尼尔·科赫

4

1 回答 1

6

“文件不是必需的架构”表示您正在尝试将目标文件与不同的架构链接:可能是 x86_64 和 i386。看起来您的 nasm 输出是 i386,请尝试使用-arch i386gcc。您还可以使用它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
于 2009-12-16T03:32:27.530 回答