我正在使用 GCC 练习 C 和汇编语言混合编程。发生了这个错误:
1 deng@ubuntu:~/workspace/leos_test$ make
2 ld -o deng c.o asm.o
3 ld: warning: cannot find entry symbol _start; defaulting to 0000000008048074
4 c.o: In function `main':
5 c_and_asm.c:(.text+0x19): undefined reference to `add'
6 c_and_asm.c:(.text+0x2e): undefined reference to `printf'
7 make: *** [deng] Error 1
这是 C 代码:
1 #include<stdio.h>
2 void extern add(int * i);
3 void main(){
4 int i=1;
5 add(&i);
6 printf("%d ",i);
7}
这是汇编语言代码:
1 .globl _add
2 _add:
3 push %bp;
4 mov %sp,%bp;
5 mov 0x4(%bp),%eax;
6 incw (%eax);
7 popw %bp;
8 ret
这是生成文件:
1 deng: c.o asm.o
2 ld -o deng c.o asm.o
3 c.o:
4 gcc -o c.o -c c_and_asm.c
5 asm.o:
6 as -o asm.o c_asm.asm
如有任何建议,我们将不胜感激:)。
最佳答案
将您的 c_asm.asm 文件更改为:
.section .text
.global add
.type add,@function
add:
push %bp;
mov %sp,%bp;
mov 0x4(%bp),%eax;
incw (%eax);
popw %bp;
ret
在你的ld中包含这个参数-lc
,最好将文件扩展名更改为.s
关于用汇编语言编写的c调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12984874/