首页 > 解决方案 > 如何将混合(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

我不明白为什么会产生这个错误。

我怎样才能摆脱这个问题?

标签: c++assemblymakefilecygwin

解决方案


您必须_按照 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 生成的代码与没有该开关生成的代码二进制不兼容。使用它来符合非默认应用程序二进制接口。并非所有目标都为此开关提供完全支持。


推荐阅读