News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Creating static libraries with MASM

Started by blackd0t, January 13, 2006, 11:18:19 AM

Previous topic - Next topic

blackd0t

Hello!

I couldn't find on Google any information on how to create a static library (.lib) with MASM. I need to create when which I can later use in Visual Studio 2005.

I would appreciate even a sourcecode of some small example library.

Regards,
Black Dot

Vortex

Hi blackd0t,

Creating a statilc library is easy :

\masm32\bin\ml /c /coff stdout.asm
\masm32\bin\ml /c /coff strlen.asm
\masm32\bin\lib /OUT:statlib.lib stdout.obj strlen.obj


...and you get statlib.lib including the two modules stdout and strlen

Using it Visual C++ :

extern void __stdcall StdOut( char * );

int main()
{
  char message[]="Hello world!";
  StdOut(message);
  return 0;
}


Notice that you have to specify the right calling convention in your function declaration. Masm uses generally the stdcallconvention.

Building the project with MS Visual C++ Toolkit 2003 :

cl Demo.c statlib.lib

Attached is the example project with all the source files.


[attachment deleted by admin]

hutch--

blackd0t,

The masm32 project has the basic template for a library module set up as a script so you just write or paste your procedure into it. Once you build a module you link it into a library or you can use POLIB as well. The m32lib directory shows how the entire masm32 lib is built so you should be able to get the idea with no real problems.

With your C++ code, you need to prototype each module making sure you use the same calling convention and the EXTERN syntax for the particular compiler.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

vg

Sorry about digging up an old post, but i can't find anything else on the topic... I'm trying to link an asm library with a C program but i get unresolved externals no matter how i put it.
I see Vortex's code was meant to be compiled&linked with VS 2003, but i only have VS 2005.
(I know VS2005 is a bit picky on the character set you use, so just in case, i used the regular 8 bits character set with the test project but it still doesn't work)
Did anyone managed to link an asm library to a C++ program using Visual Studio 2005?

hutch--

Hi vg,

Welcome on board. make sure you have done the C prototype for the asm module correctly, specify the correct calling  convention and look up the compiler's EXTERN syntax and you should be OK.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

vg

Thanks Hutch,
Well just to make sure my code is ok, here it is.
Asm code:
      .386                      ; force 32 bit code
      .model flat, stdcall      ; memory model & calling convention
      option casemap :none      ; case sensitive

.code

testproc proc var:DWORD
mov eax,var
inc eax
ret
testproc endp

end

And C code:

extern int __stdcall testproc(int);

int main(int argc, char* argv[])
{
int test = testproc(1);
return 0;
}


As you see, it couldn't be simpler ;)
The C program compiles, but doesn't link.. Is there something missing in my code?

hutch--

It depends how you give the linker access to the assembler module. You can either build your own library which is easy enough to do otherwise you will need to add it on the linker command line.

I personally prefer building a library as it makes including the assembler code easier to deal with.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Relvinian

Something to remember with C++ compilers and header files....You need to make sure everything is "C" referenced style.

Example header file for a ASM function.

#if defined(__cplusplus)
extern "C" {
#endif

// ---------------------------------------------------------------------------
// Description:
//      Initializes the memory routines.
//
//      NOTE: This function must be called before any type of memory routines
//            are called. If this function isn't, all memory routines will fail.
//
// Returns:
//      Handle of the process heap or NULL if failed to get one.
// ---------------------------------------------------------------------------
HANDLE __stdcall InitializeMemoryRoutines();



#if defined(__cplusplus)
}
#endif



Notice what I wrap my function definition around with?

Hope this helps.

Relvinian

vg

Thanks Relvinian, that worked!!  :U
All that was missing is that little "C" declaration :)