hi. i want to ask how to make an .asm file into .lib and how to use it in C++.

this is what I want to make into a .lib:
.model flat, stdcall
option casemap :none

sum proc int1,int2
mov eax,int1
add eax,int2
sum endp


then I assemble it and make into lib (I use Borland's
free command line tools = C++ 5.5) with a bat file:

\masm32\bin\ml /c "mess.asm"

set path=d:\PROGRAMY\bc55\Bin;%path%
tlib mess.lib /a mess.obj

I dont use "/coff" and I use Borland's "tlib" instead of masm's "lib" to
produce OMF lib, not COFF lib.
then i copy mess.lib into LIB directory and add a header file
to INCLUDE dir:

extern void WINAPI sum(int,int);

I have a cpp file:

#include <windows.h>
#include <mess.h>

int APIENTRY WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow

I want to compile this with:

bcc32.exe -tW "test.cpp"

and when the linker appears it writes:

Error: Unresolved external '__stdcall sum(int, int)' referenced from D:\PROGRAMY\BC55\LIBASM\TEST1\VIS\TEST.OBJ

What should I do?
Posted on 2003-12-23 11:29:59 by stolarz
In C++, you need to declare your external functions like the following:

extern "C" {
. /*All your function prototypes here*/

or even depending on your environment:

#ifdef __cplusplus
extern "C" {

header file stuff

#ifdef __cplusplus

So,you should modify your header file like this:

extern "C" void WINAPI sum(int,int);
Posted on 2003-12-23 13:01:39 by Vortex
You also need


in your ASM code.
Posted on 2003-12-23 13:49:52 by tenkey
thanks, but it still doesn't work, and the message differs a bit:

Error: Unresolved external 'sum' referenced from D:\PROGRAMY\BC55\LIBASM\TEST1\VIS\TEST.OBJ

does the compiler know that it has to take mess.lib when I write #include <mess.h> ?

I think "PUBLIC" is the default option because there was no difference in mess.obj and mess.lib after I included "PUBLIC sum" in the code.
Posted on 2003-12-23 15:53:14 by stolarz
You'll have to add the lib to your commandline, unless you use some "pragma includelib" or whatever (I recommend the commandline)
Posted on 2003-12-23 16:53:14 by f0dder
this is what I discovered:
when compiling with masm I generate a listing and there is a function "sum" but in the .lib file the real name is "_sum@8". I tried to use this function but the cpp compiler said "illegal character @". so i changed the .lib by means of a hex editor and I wrote "_suma8" inside it, changed the .h and .cpp files and everything worked. But I don't want to change every .lib I create (and i wrote lots of huge functions in asm) so my question is : is there a method to make masm not change the function names?
Posted on 2003-12-25 05:10:04 by stolarz
rather than hexing around the libs or trying to make masm not change the function names, you should figure out how to make your C/C++ compiler use the STDCALL calling convention for the functions...
Posted on 2003-12-25 05:16:43 by f0dder
... so I changed the langtype into SYSCALL: .model flat, SYSCALL. so the names in the .lib file stayed the same, without '_' and '@8'. and it was almost ok, but ..... RET !!!!! the ret instruction is usually changed to "leave + retn (bytes)" but this time it was only "leave + retn". So I was searching and I found: OPTION EPILOGUE directive. I can write what I want, I can change the function epilogue to "leave + ret (bytes)", so I wrote:

.model flat, SYSCALL ;stdcall
option casemap :none



MyEpilogue MACRO procname, flags, argbytes, localbytes, reglist,userparms:VARARG
dupa = flags AND 7
if dupa eq 2
EpilogueDef procname, flags+1, argbytes, localbytes, reglist, userparms
EpilogueDef procname, flags, argbytes, localbytes, reglist, userparms

sum proc int1,int2
mov eax,int1
add eax,int2
sum endp


where "EpilogueDef" is the default epilogue (see MASM32.HLP -> OPTION PROLOGUE).
so when the function is SYSCALL (flags AND 7 = 2) i want to call a macro for STDCALL (flags AND 7 = 3) so I call EpilogueDef with flags+1. When I assemble it, masm tells me that "EpilogueDef" macro does not exist. I tried to find this macro in every file of masm package with no effect. What should I do now?
Posted on 2003-12-25 18:43:42 by stolarz

The MASM32 library or any other that you may make with MASM is a standard Microsoft format library and can be used in Microsoft C with no problems. What you must do is write the C prototypes making sure you use the correct calling convention for each proc that you have in the library and include the libary in the list for the application.

As it has been mentioned above, make sure you use the EXTERN C syntax of the particular compiler so that you don't get problems with mangled names.

Posted on 2003-12-25 20:51:19 by hutch--