64位的Ubuntu系统上使用汇编nasm和C语言

64位的Ubuntu系统上使用汇编nasm和C语言

$ nasm -f elf foo.asm -o foo.o
$ gcc -c bar.c -o bar.o
$ ld -s  foo.o bar.o -o foobar

  ld: i386 architecture of input file `foo.o' is incompatible with i386:x86-64 output

意思是nasm 编译产生的是32位的目标代码,gcc 在64位平台上默认产生的是64位的目标代码,

这两者在链接的时候出错,gcc在64位平台上默认以64位的方式链接。

-------------------------------------------------------------------------------------------------------

方法:

让gcc 产生32位的代码,并在链接的时候以32位的方式进行链接

在这种情况下只需要修改编译和链接指令即可,具体如下:

32位的编译链接指令

$ nasm -f elf foo.asm  -o  foo.o

$ gcc -m32 -c bar.c -o bar.o

$ ld -m elf_i386 -s foo.o bar.o -o foobar

$ ./foobar
  the 2nd one

参考地址:http://www.linuxidc.com/Linux/2012-12/75804.htm

原文地址:https://www.cnblogs.com/zyx1314/p/4707195.html