2009-04-01

Create MinGW library from dll

For this, you need pexports tool from mingw-utils.
pexports library.dll > library.def
dlltool -D library.dll -d library.def -l library.dll.a
Note, that if dll was built by different environment than gcc, this probably will not work because of the naming conventions. If this the case, read here.

In my situation I've got dll build under MSVC. After executing pexports on this library, output was similar to this one:
LIBRARY library.dll
EXPORTS
DllRegisterServer
DllUnregisterServer
func_count
func_decrypt_data
func_encrypt_data
func_exit
...
Getting library.a from this def file is no problem. But during the linking I've got errors about undefined references. Surfing the internet and reading the link above has helped, and after manually editing the definition file I've finally was able to get everything working. The only thing you need to add is the @xx part, where xx is the size of the function arguments, in bytes:
LIBRARY library.dll

EXPORTS

; dll register functions
DllRegisterServer
DllUnregisterServer

; public functions
func_count@4
func_decrypt_data@16
func_encrypt_data@16
func_exit@0
...
Note, that not all functions from def file available to use. Appearantly, import table contains private functions too. So, you need to add ordinals only for public functions (or the ones you will use).
Now, the only question is about how you can get arguments size for particular function without calculating for yourself. Answer is simple: you can't, dude! I have tried numerous tools for that and didn't found an answer except that linker will shoot an error with mentioned ordinals (the same as in modified def file). That's your choice: size calculation for every function, or putting all of them in code so the linker will give your ordinals you need.
Finally, correct dlltool parameters for def with ordinals are:
dlltool -k -D library.dll -d library.def -l library.dll.a

No comments:

Post a Comment