我有这个简单的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;
}
而且我有这个s.asm,它定义了函数_Sum:
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',因此进行更改即可解决问题。
我有这个简单的C源代码:#include
据我从您的问题中可以看到的,您用C程序覆盖了来自汇编器s.o
的目标文件。因此,您再也不需要汇编程序了。
您可能应该写
在典型的汇编程序中,标签默认为本地。要告诉汇编器使它们对外部例程可见,必须添加一个声明,例如:
最好在c文件中内联声明asm。这是我自己的代码中的一个示例: