您的位置:首页 > 编程语言 > C语言/C++

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

2015-08-06 10:10 357 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: