在OSX中gcc使用的文件格式是什么?

时间:2009-12-16 03:27:20

标签: macos gcc assembly osx-snow-leopard nasm

我正在尝试使用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的正确格式是什么?

谢谢。 丹尼尔科赫

1 个答案:

答案 0 :(得分:5)

“文件不是必需的体系结构”表示您正在尝试链接具有不同体系结构的目标文件:可能是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