ld: i386 架构于输入文件foo.o 与 i386:x86-64 输出不兼容

报错:ld: i386 架构于输入文件 foo.o 与 i386:x86-64 输出不兼容

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

编译链接指令

nasm -f elf foo.s -o foo.o

gcc -c bar.c -o bar.o

ld -s -o foobar bar.o foo.o

汇编语言用nasm编写并用nasm编译器编译,而C语言用的是gcc编译,这些都没有问题,但是在链接的时候出错了,提示如下:
ld: i386 architecture of input file `foo.o' is incompatible with i386:x86-64 output


意思就是nasm 编译产生的是32位的目标代码,gcc 在64位平台上默认产生的是64位的目标代码,这两者在链接的时候出错,gcc在64位平台上默认以64位的方式链接。
这样在解决的时候就会有两种解决方案:
<1> 让gcc 产生32位的代码,并在链接的时候以32位的方式进行链接
在这种情况下只需要修改编译和链接指令即可,具体如下:
32位的编译链接指令

nasm -f elf foo.s -o foo.o

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

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

具体的-m32 和 -m elf_i386 请自行查阅gcc (man gcc)

如果你是高版本的gcc(可能是由于更新内核造成的),可能简单的使用-m32 的时候会提示以下错误(使用别人的历程,自己未曾遇到):
> In file included from /usr/include/stdio.h:28:0,
> from test.c:1:
> /usr/include/features.h:323:26: fatal error: bits/predefs.h: No such file or directory
> compilation terminated.
这应该是缺少构建32 位可执行程序缺少的包,使用以下指令安装:
sudo apt-get install libc6-dev-i386
此时应该就没有什么问题了。

 

参考:https://zhidao.baidu.com/question/1831399869227285020.html

posted @ 2017-04-03 18:37  jenningszheng  阅读(13919)  评论(1编辑  收藏  举报