【问题标题】:Problems compiling assembly file - Error: undefined reference to `function name'编译程序集文件时出现问题 - 错误:未定义对“函数名”的引用
【发布时间】:2014-09-26 04:17:59
【问题描述】:

我正在尝试查看我的教授给我们的测试程序,但我无法编译它。我在 Ubuntu 14.04 上。我正在用

编译它
gcc -Wall test.c AssemblyFunction.S -m32 -o test

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

这里是 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

感谢您的宝贵时间。我一直在努力解决这个问题好几个小时,所以我很感激任何帮助。

【问题讨论】:

    标签: c assembly compiler-errors


    【解决方案1】:

    我相信使用 GCC,您会想要删除汇编文件中的 _。所以这些行:

    .global _addnumbersinAssembly
    _addnumbersinAssembly:
    

    应该是:

    .global addnumbersinAssembly
    addnumbersinAssembly:
    

    可以在 StackOverflow question/answer 中找到有关此问题的更多信息。

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

    【讨论】:

    • 哇,谢谢!如果您不介意我问,“_”是什么意思。教授只是告诉我们把它放进去,根本没有解释那些标题行。
    • C 编译器通常遵循自动在全局(非静态)变量/函数上附加下划线的旧约定,以防止与 C 运行时库发生名称冲突(初始化内存的代码设置运行环境并执行你的 main() 函数)。他们不希望人们在他们的.c 中创建符号,这些符号可能会(意外地)替换允许 C 程序运行的基本代码(变量和函数)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多