简体   繁体   English

在MASM中调用C ++函数

[英]Calling C++ functions in MASM

I'm working on a program that will use MASM to call some C++ functions. 我正在开发一个使用MASM调用某些C ++函数的程序。 I defined, in a separate file, to sum 2 integers and display the output. 我在一个单独的文件中定义了对2个整数求和并显示输出。

Currently, I can't get 'main.cpp' to run asmMain() to call functions from 'main.cpp'. 目前,我无法让'main.cpp'运行asmMain()从'main.cpp'调用函数。

code.asm 代码库

; ---------------------------------------

promptFirst PROTO C
promptSecond PROTO C
printInt PROTO C

.586
.model flat, stdcall

.stack 4096

; ---------------------------------------

.DATA

first DWORD 0
second DWORD 0

; --------------------------------

.CODE

asmMain PROC C  
    mov first, promptFirst              
    ret 
asmMain ENDP

PUBLIC asmMain  
END

main.cpp main.cpp

#include <iostream>

using namespace std;

void asmMain();

int promptFirst();
int promptSecond();
void printInt(int myint);

int main() {
    asmMain();
}

int promptFirst() {
    cout << " The first number = ";
    int newint;
    cin >> newint;

    return newint;
}

int promptSecond() {
    cout << "\nThe second number = ";
    int newint;
    cin >> newint;

    return newint;
}

void printInt(int myint) {
    cout << myint;
}

The error I get for the current code is such: 我得到的当前代码错误是这样的:

Build started: Project: Project_Name, Configuration: Debug Win32 开始构建:项目:Project_Name,配置:调试Win32
main.cpp main.cpp
code.obj: error LNK2019: unresolved external symbol _promptFirst referenced in function _asmMain code.obj:错误LNK2019:函数_asmMain中引用的未解析的外部符号_promptFirst

Any hints on how to resolve this? 关于如何解决此问题的任何提示?

The problem is that C++ compilers mangles the symbols , which is one of the reasons behind the extern "C" construct, so that symbols are not mangled. 问题在于C ++编译器会破坏符号 ,这是extern "C"构造背后的原因之一,因此不会破坏符号。

If you declare the function to be extern "C" then the compiler will not mangle the name, just like you do with the assembler function you call. 如果将函数声明为extern "C"则编译器将不会修改名称,就像对调用的汇编器函数所做的那样。

Use dumpbin.exe on your object-file to get the mangled name of your C++ functions. 在目标文件上使用dumpbin.exe获取C ++函数的错误名称。

Or ask for a symbol with C-linkage using extern "C" on the function-declarations in your C++ code. 或使用C ++代码中的函数声明中的extern "C"来请求带有C链接的符号。

Then you will know what to call in your assembly-code. 然后,您将知道在汇编代码中要调用的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM