c++ - 如何将混合(asm、C++)源代码编译成 32 位程序?
问题描述
我在 64 位机器上使用 Win7 下的Cygwin 32位。
以下程序
生成文件:
runme: main.cpp asm.o
g++ main.cpp asm.o -o executable
asm.o: asm.asm
nasm -f elf asm.asm -o asm.o
asm.asm:
section .data
section .bss
section .text
global GetValueFromASM
GetValueFromASM:
mov eax, 9
ret
主.cpp:
#include <iostream>
using namespace std;
extern "C" int GetValueFromASM();
int main()
{
cout<<"GetValueFromASM() returned = "<<GetValueFromASM()<<endl;
return 0;
}
给我以下错误:
$ make
nasm -f elf asm.asm -o asm.o
g++ main.cpp asm.o -o executable
/tmp/cc3F1pPh.o:main.cpp:(.text+0x26): undefined reference to `GetValueFromASM'
collect2: error: ld returned 1 exit status
make: *** [makefile:2: runme] Error 1
我不明白为什么会产生这个错误。
我怎样才能摆脱这个问题?
解决方案
您必须_
按照 Windows/Cygwin 中的惯例为符号添加前缀:
section .data
section .bss
section .text
global _GetValueFromASM
_GetValueFromASM:
mov eax, 9
ret
您的其余代码应该可以正常工作。
另一种方法是使用-fno-leading-underscore
. 但是,这可能会中断与其他(Cygwin 系统)库的链接。如果对其他平台的可移植性对您来说不重要,我建议使用第一个选项。
引用 GNU 在线文档:
-fleading-underscore
此选项及其对应选项
-fno-leading-underscore
强制更改 C 符号在目标文件中的表示方式。一种用途是帮助链接旧的汇编代码。警告:该
-fleading-underscore
开关导致 GCC 生成的代码与没有该开关生成的代码二进制不兼容。使用它来符合非默认应用程序二进制接口。并非所有目标都为此开关提供完全支持。
推荐阅读
- python - np.sin(myArray) 的 numpy Typerror
- qt - 在 Qt 中,使用不在源目录中的资源
- c++ - 列出擦除迭代器超出范围
- coldfusion - Mura 在页面中显示表单数据
- ionic-framework - 在 Ionic 中设置页面加载的焦点
- android - 实例化 WiFiAwareManager 对象
- imap - 有没有办法使用 Python 3 IMAPlib 来检索给定消息的发件人地址和关联的 UID
- android - Android Studio 未解析 MediaMetadataRetriever.getFramesAtIndex(int, int)
- reactjs - Reactjs 表。如何将多个值加入到 1 个单元格中?
- c - 检查元素是否在数组中或不是无限循环