Skip to content

Instantly share code, notes, and snippets.

@guilt
Last active December 13, 2023 14:03
Show Gist options
  • Save guilt/063d5cb945bef1a31642a4fd4e19d458 to your computer and use it in GitHub Desktop.
Save guilt/063d5cb945bef1a31642a4fd4e19d458 to your computer and use it in GitHub Desktop.
Generate Import Libraries from a DLL
@echo off
REM Usage: dll2lib some-file.dll
REM
REM Generates libsome-lib.a, some-file.lib from some-file.dll,
REM making an intermediate some-file.def from the results of
REM dumpbin /exports some-file.dll.
REM
REM Currently must run without path on DLL.
REM (Fix by removing path when of lib_name for LIBRARY line below?)
REM
REM Requires 'dumpbin' and 'lib' in PATH - run from VS developer prompt.
REM Requires 'dlltool' in PATH - run from MinGW developer prompt.
REM
REM Script inspired by http://stackoverflow.com/questions/9946322/how-to-generate-an-import-library-lib-file-from-a-dll
if not exist "%1" (goto End)
setlocal
set dll_file=%1
set dll_file_no_ext=%dll_file:~0,-4%
set exports_file=%dll_file_no_ext%-exports.txt
set def_file=%dll_file_no_ext%.def
set lib_name=%dll_file_no_ext%
set lib_file=%dll_file_no_ext%.lib
set mlib_file=lib%dll_file_no_ext%.a
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%)
REM Generate Libaries
lib /nologo /def:%def_file% /out:%lib_file%
dlltool --input-def %def_file% --output-lib %mlib_file%
REM Clean up temporary intermediate files
del %exports_file% %dll_file_no_ext%.exp
REM Clean up def file
del %def_file%
:End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment