English 中文(简体)
What s the file format used by gcc in OSX?
原标题:

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

最佳回答

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
问题回答

暂无回答




相关问题
gcc -fPIC seems to muck with optimization flags

Following along from this question: how-do-i-check-if-gcc-is-performing-tail-recursion-optimization, I noticed that using gcc with -fPIC seems to destroy this optimization. I am creating a shared ...

Generate assembler code from C file in linux

I would like to know how to generate assembler code from a C program using Unix. I tried the gcc: gcc -c file.c I also used firstly cpp and then try as but I m getting errors. I m trying to build an ...

Getting rid of pre-compiled headers

OK, I have old Metrowerks code for Mac and Windows where the previous developer used pre-compiled headers for every project that this code base builds. How does one get rid of Pre-compiled headers, ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

How to compile for Mac OS X 10.5

I d like to compile my application for version 10.5 and forward. Ever since I upgraded to Snow Leopard and installed the latest XCode, gcc defaults to 10.6. I ve tried -isysroot /Developer/SDKs/...

热门标签