我無法鏈接2個對象文件,其中一個是從彙編語言源文件生成的,另一個是從C源文件生成的。如何鏈接C對象文件和彙編語言對象文件?
C源代碼:
//main2.c
extern int strlength(char *);
int main(){
char * test = "hello";
int num = strlength(test);
return num;
}
彙編源代碼:
#strlength.s
.include "Linux32.s"
.section .text
.globl strlength
.type strlength, @function
strlength:
pushl %ebp
movl %esp, %ebp
movl $0, %ecx
movl 8(%ebp), %edx
read_next_byte:
movb (%edx), %al
cmpb $END_OF_FILE, %al
jle end
incl %edx
incl %ecx
jmp read_next_byte
end:
movl %ecx, %eax
popl %ebp
ret
當我編譯和運行程序使用 'GCC' 是這樣的:
gcc main2.c strlength.s -m32 -o test
./test
echo $?
我得到5,它是正確。然而,當我編譯/彙編分開,然後用「LD」 這樣的鏈接:
as strlength.s --32 -o strlength.o
cc main2.c -m32 -o main2.o
ld -melf_i386 -e main main2.o strlength.o -o test
./test
我得到一個分段錯誤。這是什麼造成的?我沒有正確遵循C調用約定嗎?