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

发布于 2024-08-15 04:31:55 字数 1714 浏览 2 评论 0原文

我正在尝试使用 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

如果我尝试仅使用 gcc -c in driver.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 的正确格式是什么?

谢谢。 丹尼尔·科赫

I'm trying to learn assembly using NASM, the pcasm-book.pdf from Dr Paul Carter - http://www.drpaulcarter.com/pcasm/ - on my Mac OS X Snow Leopard.

I'm trying to link the previous compiled C sample to asm samples:

gcc first.o driver.c asm_io.o -o first

But it's returning it:

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

I'm using the Mach-o format to compile asm samples, and I got no errors:

nasm -f macho **first.asm**
nasm -f macho asm_io.asm

If I try to use only gcc -c in driver.c, using ld to link all object files, ld appears to not link driver.o format.

ld -o first asm_io.o first.o driver.o

It returns:

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

What's the problem? What is the correct format to work with gcc and NASM on OS X?

Thank you.
Daniel Koch

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2024-08-22 04:31:55

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

The "file is not of required architecture" indicates that you're trying to link object files with different architectures: probably x86_64 and i386. As it appears your nasm output is i386, try using -arch i386 with gcc. You can also use file to display the architecture of a given object file or library.

% 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文