【发布时间】:2013-04-16 19:06:10
【问题描述】:
我有这个简单的 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”,因此更改解决了问题。
【问题讨论】:
-
就在我的脑海中,您的 sum 原型不正确。
extern int Sum(int,int); -
您可能需要在 asm 端的某处放置
global _Sum -
我试过
global _Sum而不是_Sum,我得到了同样的错误信息@harold -
我还将原型更改为
extern int Sum(int,int);而不是旧的,我收到了相同的错误消息@RageD -
gcc 的“-o”选项不是用于指定输出文件吗?在这种情况下,test.c 的编译可能会覆盖 s.o 文件?
标签: c linux assembly nasm linker-errors