简体   繁体   中英

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. I have managed to make a .lib file from assembly MASM. But i have no idea how to call it to C language as a library. Here is the .lib file 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.
  2. Write a C-compatible prototype/declaration for each function.
  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:

#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. ESPECIALLY the "c". 64 bit masm assembly doesn't need it for some reason it just works. 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.

It's some kind of weird legacy 32 bit thing versus the 64 bit.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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