简体   繁体   English

将MASM中制作的库调用到C或C ++

[英]Calling a library made in MASM to C or C++

i have been trying to call a library in c which was made in masm. 我一直试图在C中调用由masm制作的库。 I have managed to make a .lib file from assembly MASM. 我设法从程序集MASM制作一个.lib文件。 But i have no idea how to call it to C language as a library. 但我不知道如何将其称为C语言库。 Here is the .lib file https://www.dropbox.com/s/d9d8cjbxmo51yqg/main.lib 这是.lib文件https://www.dropbox.com/s/d9d8cjbxmo51yqg/main.lib

Help needed. 需要帮助。 Thanks 谢谢

The basic idea is fairly simple: 基本思想很简单:

  1. Write the (externally visible) assembly language functions using the C calling convention. 使用C调用约定编写(外部可见的)汇编语言函数。
  2. Write a C-compatible prototype/declaration for each function. 为每个函数编写一个与C兼容的原型/声明。
  3. Call functions as needed. 根据需要调用函数。

The general idea looks something like this (warning: untested code): 一般想法如下所示(警告:未经测试的代码):

; masm file
.model flat, c

.code

plus1 proc input:dword
    mov eax, input
    add eax, 1
    ret
plus1 endp
     end

C/C++ header: C / C ++标头:

#ifdef __cplusplus
extern "C" {
#endif

int plus1(int);

#ifdef __cplusplus
}
#endif

Calling code: 调用代码:

#include "header.h"

int main() { 
   int x = plus1(14);
}

Oh man. 天啊。 Huge gotcha on this. 巨大的陷阱。 In a 32bit masm .asm file in visual studio, that ".model flat, c" is CRITICAL. 在Visual Studio中的32位masm .asm文件中,“。model flat,c”是至关重要的。 ESPECIALLY the "c". 特别是“ c”。 64 bit masm assembly doesn't need it for some reason it just works. 64位masm汇编由于某种原因不需要它,因此可以正常工作。 But if you try to call extern "C" defined functions from your C++ or C code in your assembly, the 32 bit won't link and it will complain about unresolved symbols, regardless of defining EXTERN symbol: PROC in your asm. 但是,如果您尝试从程序集中的C ++或C代码中调用extern“ C”定义的函数,则32位将不会链接,并且它将抱怨未解析的符号,无论在您的asm中定义了EXTERN符号:PROC。

It's some kind of weird legacy 32 bit thing versus the 64 bit. 与64位相比,这是一种奇怪的传统32位。

Beside that, for your question, I think you just want to declare your asm functions PUBLIC, just "PUBLIC functionname" in your asm, and then you don't need a header or anything for them, just define them with "extern" or extern "C" in your calling C/C++ code and it will find them in the assembly object and link. 除此之外,对于您的问题,我想您只想在asm中声明您的asm函数PUBLIC,只需声明“ PUBLIC函数名”,然后就不需要它们的标头或任何内容,只需使用“ extern”或在调用的C / C ++代码中使用extern“ C”,它将在汇编对象和链接中找到它们。

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

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