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

后端 未结 3 1554
不知归路
不知归路 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: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 
    
    .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.

提交回复
热议问题