How to generate an import library (LIB-file) from a DLL? How to generate an import library (LIB-file) from a DLL? windows windows

How to generate an import library (LIB-file) from a DLL?


You can generate a DEF file using dumpbin /exports:

echo LIBRARY SQLITE3 > sqlite3.defecho EXPORTS >> sqlite3.deffor /f "skip=19 tokens=4" %A in ('dumpbin /exports sqlite3.dll') do echo %A >> sqlite3.def

The librarian can use this DEF file to generate the LIB:

lib /def:sqlite3.def /out:sqlite3.lib /machine:x86

All of the filenames (sqlite3.dll, sqlite3.def, etc.) should be prepended with full paths.


I know the topic is old, but I still couldn't find a script or batch file anywhere on the Internet to do this. So based on Dark Falcon's answer, I've made this script, which you can save as dll2lib.bat and run:

REM Usage: dll2lib [32|64] some-file.dllREMREM Generates some-file.lib from some-file.dll, making an intermediateREM some-file.def from the results of dumpbin /exports some-file.dll.REM Currently must run without path on DLL.REM (Fix by removing path when of lib_name for LIBRARY line below?)REMREM Requires 'dumpbin' and 'lib' in PATH - run from VS developer prompt.REM REM Script inspired by http://stackoverflow.com/questions/9946322/how-to-generate-an-import-library-lib-file-from-a-dllSETLOCALif "%1"=="32" (set machine=x86) else (set machine=x64)set dll_file=%2set dll_file_no_ext=%dll_file:~0,-4%set exports_file=%dll_file_no_ext%-exports.txtset def_file=%dll_file_no_ext%.defset lib_file=%dll_file_no_ext%.libset lib_name=%dll_file_no_ext%dumpbin /exports %dll_file% > %exports_file%echo LIBRARY %lib_name% > %def_file%echo EXPORTS >> %def_file%for /f "skip=19 tokens=1,4" %%A in (%exports_file%) do if NOT "%%B" == "" (echo %%B @%%A >> %def_file%)lib /def:%def_file% /out:%lib_file% /machine:%machine%REM Clean up temporary intermediate filesdel %exports_file% %def_file% %dll_file_no_ext%.exp

I'm sure the script can use improvement, but I hope it's useful.


This script creates *.lib from *.dll passed in %1:

@echo offsetlocal enabledelayedexpansionfor /f "tokens=1-4" %%1 in ('dumpbin /exports %1') do (    set /a ordinal=%%1 2>nul    set /a hint=0x%%2 2>nul    set /a rva=0x%%3 2>nul    if !ordinal! equ %%1 if !hint! equ 0x%%2 if !rva! equ 0x%%3 set exports=!exports! /export:%%4)for /f %%i in ("%1") do set dllpath=%%~dpnistart lib /out:%dllpath%.lib /machine:x86 /def: %exports%

You could name it implib.bat and run: implib.bat C:\folder\mydll.dll which produces C:\folder\mydll.lib