在内联程序集中定义函数和从 C++ 调用时出现问题

Issue defining a function in inline assembly and calling from c++

本文关键字:调用 C++ 问题 函数 程序 程序集 定义 集中      更新时间:2023-10-16

我正在尝试编译以下链接中提到的代码。 我收到以下链接器错误:

/tmp/ccUVLIZ0.ltrans0.ltrans.o: 在函数 'main' 中:

:(.text.startup+0x5(:对"一"的未定义引用

collect2:错误:ld 返回 1 个退出状态

等效于 GCC 的裸属性

链接器未看到程序集定义?

代码如下:

#include <stdio.h>
asm("_one:              n
movl $1,%eax    n
ret             n
");
int one();
int main() {
printf("result: %dn", one());
return 0;
}

对于这样的技巧,你需要显式提供函数规范

#include <stdio.h>
asm("one:              n
movl $1,%eax    n
ret             n
");
extern "C" int one();
int main() {
printf("result: %dn", one());
return 0;
}

您可能可以在以下位置找到有关外部"C"的更多解释 外置"C"在C++的影响是什么?