gpt4 book ai didi

c - 编译程序集文件时出现问题 - 错误 : undefined reference to `function name'

转载 作者:太空狗 更新时间:2023-10-29 16:12:14 24 4
gpt4 key购买 nike

我正在尝试查看我的教授给我们的测试程序,但我在编译它时遇到了问题。我在 Ubuntu 14.04 上。我正在编译它

gcc -Wall test.c AssemblyFunction.S -m32 -o test

我在 64 位机器上运行代码时遇到问题,并阅读了添加 -Wall 和 -m32 将允许它工作。这样做解决了我遇到的第一个问题,但现在我收到了错误:对 `addnumbersinAssembly' 的 undefined reference

这是C文件

#include <stdio.h>
#include <stdlib.h>

extern int addnumbersinAssembly(int, int);
int main(void)
{

int a, b;
int res;

a = 5;
b = 6;


// Call the assembly function to add the numbers
res = addnumbersinAssembly(a,b);
printf("\nThe sum as computed in assembly is : %d", res);

return(0);
}

这是汇编文件

.global _addnumbersinAssembly
_addnumbersinAssembly:
pushl %ebp
movl %esp,%ebp


movl 8(%ebp), %eax
addl 12(%ebp), %eax # Add the args


movl %ebp,%esp
popl %ebp
ret

感谢您的宝贵时间。几个小时以来,我一直在努力解决这个问题,因此非常感谢您的帮助。

最佳答案

我相信对于 GCC,您会希望删除汇编文件中的 _。所以这些行:

.global _addnumbersinAssembly
_addnumbersinAssembly:

应该是:

.global addnumbersinAssembly
addnumbersinAssembly:

有关此问题的更多信息,请参见 StackOverflow question/answer .

-m32 编译参数是必需的,因为您需要重写汇编代码以支持某些 64 位操作。在您的情况下,它是堆栈操作。 -Wall 不需要编译,但它会打开更多警告。

关于c - 编译程序集文件时出现问题 - 错误 : undefined reference to `function name' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26051946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com