ld: i386 architecture of input file is incompatible with i386:x86-64 output

如果报以上错误,可以使用下面的语句来解决:

If want compile the file as 32 bits, you can use:

ld -m elf_i386 -s -o file file.o

混合编译C语言和汇编语言,都需要指明32位,如下:

有两个文件foo.asm bar.c

foo.asm


; 编译链接方法
; (ld 的‘-s’选项意为“strip all”)
;
; [root@XXX XXX]# nasm -f elf foo.asm -o foo.o
; [root@XXX XXX]# gcc -c bar.c -o bar.o
; [root@XXX XXX]# ld -s hello.o bar.o -o foobar
; [root@XXX XXX]# ./foobar
; the 2nd one
; [root@XXX XXX]# 


extern choose	; int choose(int a, int b);


[section .data]	; 数据在此

num1st		dd	3
num2nd		dd	4


[section .text]	; 代码在此

global _start	; 我们必须导出 _start 这个入口,以便让链接器识别。
global myprint	; 导出这个函数为了让 bar.c 使用

_start:
	push	num2nd		; ┓
	push	num1st		; ┃
	call	choose		; ┣ choose(num1st, num2nd);
	add	esp, 4		; ┛

	mov	ebx, 0
	mov	eax, 1		; sys_exit
	int	0x80		; 系统调用

; void myprint(char* msg, int len)
myprint:
	mov	edx, [esp + 8]	; len
	mov	ecx, [esp + 4]	; msg
	mov	ebx, 1
	mov	eax, 4		; sys_write
	int	0x80		; 系统调用
	ret
	

bar.c


void myprint(char* msg, int len);

int choose(int a, int b)
{
	if(a >= b){
		myprint("the 1st one\n", 13);
	}
	else{
		myprint("the 2nd one\n", 13);
	}

	return 0;
}

两个文件相互调用,其编译方法为:

nasm -f elf -o foo.o foo.asm
gcc -m32 -c bar.c -o bar.o

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灰暗角落里的琴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值