【发布时间】:2014-04-03 20:48:05
【问题描述】:
我的程序有问题。当我尝试使用此行编译带有汇编函数的 C 程序时:
gcc -o program factarial.c
我明白了,但我不知道为什么:
/tmp/ccFKqbDP.o: In function `main':
factarial.c:(.text+0x11): undefined reference to `factarial_asm'
collect2: ld returned 1 exit status
这是我的 C 代码:
#include <stdio.h>
extern void factarial_asm();
int main ()
{
factarial_asm (5);
return 0;
}
这是汇编代码:
.data
.text
.global _main
.type factarial_asm, @function
factarial_asm:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
cmpl $1, %eax
je koniec
decl %eax
pushl %eax
call factarial_asm
movl 8(%ebp), %ebx
mull %ebx
koniec:
leave
ret
另外,当我尝试使用此行使用 C 函数编译 asm 代码时:
gcc -o program factarial.s
我遇到了这个问题:
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/ccWCmTBe.o: In function `main':
(.text+0x3): undefined reference to `factarial'
collect2: ld returned 1 exit status
这是我的汇编代码:
SYSEXIT = 1
EXIT_SUCC = 0
SYSWRITE = 4
SYSCALL = 0x80
SYSREAD = 3
.align 32
.text
.global _main
main:
pushl $5
call factarial
movl $SYSEXIT, %eax
movl $EXIT_SUCC, %ebx
int $SYSCALL
还有我的 C 代码:
#include <stdio.h>
int factarial(int n)
{
if(n == 0) return 1;
else return n * factarial(n - 1);
}
我知道有很多问题,但到目前为止我正在准备编译器和 ld 链接器,所以我不完全知道如何使用 gcc。 另外,谁能帮我准备makefile文件?
【问题讨论】: