我有这个简单的 C 源代码:
#include <stdio.h>
extern int Sum(int,int);
int main()
{
int a,b,s;
a=1 , b=2;
s = Sum(a,b);
return 0;
}
我有这个定义函数 _Sum 的 s.asm :
global _Sum
_Sum:
push ebp ; create stack frame
mov ebp, esp
mov eax, [ebp+8] ; grab the first argument
mov ecx, [ebp+12] ; grab the second argument
add eax, ecx ; sum the arguments
pop ebp ; restore the base pointer
ret
现在,我使用以下方法编译了 .asm:
nasm s.asm -f elf -o s.o
并使用以下方法编译和链接 .c 文件:
gcc s.o test.o -o testapp
这是结果:
/tmp/ccpwYHDQ.o: In function `main':
test.c:(.text+0x29): undefined reference to `Sum'
collect2: ld returned 1 exit status
那么问题是什么?
我正在使用 Ubuntu-Linux
任何帮助将不胜感激,谢谢
[已解决]:我用 nm 检查了 test.o 文件,它预计会找到符号“Sum”而不是“_Sum”,因此更改解决了问题。