【问题标题】:I get an error trying to compile assembly code/C++ with G++尝试使用 G++ 编译汇编代码/C++ 时出错
【发布时间】:2022-10-02 23:30:33
【问题描述】:

所以我写了一个小程序来测试一切是否正常。 它应该需要两个输入和输出它们的总和。

测试.cpp:

#include <iostream>
#include <stdio.h>

extern \"C\" int test(int a, int b);

int main(){
    int x = 0;
    std::cout << test(10, 20);
    std::cin >> x;
    return 0;
}

测试.s:

.global test

test:

    mov %eax, %ecx
    add %eax, %edx

    ret

然后我尝试用 g++ 编译它:g++ -o main.exe test.cpp但我得到一个错误:对“测试”的未定义引用

我对汇编编程完全陌生。有什么建议吗?

  • 您的编译命令只编译\"test.cpp\",但您还需要编译和链接\"test.s\",以便解析对test 的引用

标签: c++ assembly


【解决方案1】:

您必须在构建命令中包含 test.s 文件:

g++ -o main.exe test.cpp test.s

否则编译器会抱怨 test 函数未定义,因为它找不到它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    相关资源
    最近更新 更多