how to use a library in masm or more specifcally a .lib file?

后端 未结 3 1553
不知归路
不知归路 2021-01-16 02:00

I have made a .lib file using visual studio 2010 and now I want to use it in masm. How can I do that? need help. I tried to look it on the internet but couldn\'t find any pr

相关标签:
3条回答
  • 2021-01-16 02:05

    I was too optimistic saying that there are many examples for a MASM-C-combination in the net. Here a step-by-step instruction for Visual Studio Express 2010 (no Masm32):

    MathFuncsLib.cpp:

    extern "C" {
        int Addition (int a, int b) { return a + b; }
        int Subtraction (int a, int b) { return a - b; }
        int Multiplication (int a, int b) { return a * b; }
        int Division (int a, int b) { return a / b; }
    } // end of extern "C"
    

    MathFuncsLib.asm:

    .MODEL flat, stdcall
    
    includelib kernel32.lib
    ExitProcess PROTO STDCALL, :DWORD
    
    includelib msvcrt.lib
    printf PROTO C, :VARARG
    
    includelib MathFuncsLib.lib
    Addition PROTO C, :SDWORD, :SDWORD
    Subtraction PROTO C, :SDWORD, :SDWORD
    Multiplication PROTO C, :SDWORD, :SDWORD
    Division PROTO C, :SDWORD, :SDWORD
    
    .data
        arg1 SDWORD 100
        arg2 SDWORD 10
        result SDWORD ?
    
        fmt1 db "Addition: %u",10,0
        fmt2 db "Subtraction: %u",10,0
        fmt3 db "Multiplication: %u",10,0
        fmt4 db "Division: %u",10,0
    
    .code
    main PROC
    
        invoke Addition, arg1, arg2
        mov result, eax
        invoke printf, ADDR fmt1, result        ; printf (fmt1,result)
    
        invoke Subtraction, arg1, arg2
        mov result, eax
        invoke printf, ADDR fmt2, result
    
        invoke Multiplication, arg1, arg2
        mov result, eax
        invoke printf, ADDR fmt3, result
    
        invoke Division, arg1, arg2
        mov result, eax
        invoke printf, ADDR fmt4, result
    
        invoke ExitProcess, 0
    
    main ENDP
    
    END main
    

    MathFuncsLib.cmd:

    @ECHO OFF
    call "C:\Compiler\Visual Studio Express 2010\VC\vcvarsall.bat"
    rem Adapt it with path to your 'vcvarsall.bat'
    
    cl.exe MathFuncsLib.cpp /c
    lib.exe MathFuncsLib.obj
    ml.exe MathFuncsLib.asm /link /subsystem:console
    
    MathFuncslib.exe
    

    Good luck!

    0 讨论(0)
  • 2021-01-16 02:15

    Masm615 is a 16 bit assembler and is incompatible with .lib files generated with 32/64 bit compilers like VS. A better option would be to use a 16 bit compiler like Turbo C++.

    0 讨论(0)
  • 2021-01-16 02:17

    Your MathFuncsLib.lib includes decorated symbols and references to other .libs.

    MSVC "decorate" its C++-symbols, i.e. the symbols contain not only names but also arguments, types, etc. - the whole declaration. If you call DUMPBIN.EXE MathFuncsLib.lib /SYMBOLS you can see the symbols stored in the .lib and find:

    067 00000000 SECT19 notype () External | ?Subtract@MyMathFuncs@MathFuncs@@SAHHH@Z (public: static int __cdecl MathFuncs::MyMathFuncs::Subtract(int,int))

    ?Subtract@MyMathFuncs@MathFuncs@@SAHHH@Z is the decorated symbol and (public: static int __cdecl MathFuncs::MyMathFuncs::Subtract(int,int)) its undecorated content. MASM can't automatically undecorate the symbols, so you must manually explore the symbol and take the whole symbol in a PROTO directive. A following equate is useful for later calls.

    It isn't generally a good idea to mix C++ and Assembler especially when using objects. Better is to write a C-program (C and C++ are two different languages!) and to integrate those .libs into an Assembler program. There are many examples for this in the net.

    I've got following code running:

    test.asm:

    include \masm32\include\masm32rt.inc        ; MASM32 headers especially 'pr2'
    
    includelib msvcprtd.lib
    includelib MathFuncsLib.lib
    
    ?Subtract@MyMathFuncs@MathFuncs@@SAHHH@Z PROTO SYSCALL
    Subtract EQU <pr2 PTR ?Subtract@MyMathFuncs@MathFuncs@@SAHHH@Z>
    
    .data
        int1 SDWORD 100
        int2 SDWORD 10
        result SDWORD ?
    
    .code
    main PROC
    
        invoke Subtract, int1, int2
        add esp, 8
        mov result, eax
    
        printf ("%i\n",result);
    
        invoke ExitProcess, 0
    
    main ENDP
    
    END main
    

    test.cmd:

    @ECHO OFF 
    SET PATH=C:\masm32\bin; 
    SET LIB=C:\TMP\;C:\Compiler\Visual Studio Express 2010\VC\lib; 
    ml.exe /nologo /coff test.asm /link /subsystem:console /opt:noref /ignore:4044 /nologo 
    test.exe
    

    In my testcase C:\TMP is the path to MathFuncsLib.lib. C:\Compiler\Visual Studio Express 2010\VC\lib is the path to msvcprtd.lib.

    0 讨论(0)
提交回复
热议问题