Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is it possible to autogenerate a MSVC import library (LIB-file) from a DLL? How?

like image 658
Albert Avatar asked Mar 30 '12 15:03

Albert


People also ask

How do I convert a DLL to a library?

You could export them by adding __declspec(dllexport) to the function declarations, or you can add a "Module-Definition File (. def)" to the project, and list the functions to export. For example here a function foo can be from a static lib included in my DLL.

How do I create a .LIB file?

On the Application Settings page of the Win32 Application Wizard, under Application type, select Static library. On the Application Settings page of the Win32 Application Wizard, under Additional options, clear the Precompiled header check box. Press Finish to create the project.

What is DLL import library?

An import library (. lib) file contains information the linker needs to resolve external references to exported DLL functions, so the system can locate the specified DLL and exported DLL functions at run time. You can create an import library for your DLL when you build your DLL.

Does a DLL need a lib file?

lib must be passed to the linker. The second one is explicit linking when we use the DLL by manually loading it with LoadLibrary function. In this type we don't need that . lib file, but we must put a little effort to find DLL exports, their addresses, and call these functions through pointers.


2 Answers

You can generate a DEF file using dumpbin /exports:

echo LIBRARY SQLITE3 > sqlite3.def echo EXPORTS >> sqlite3.def for /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.

like image 50
Dark Falcon Avatar answered Sep 22 '22 16:09

Dark Falcon


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.dll REM REM Generates some-file.lib from some-file.dll, making an intermediate REM 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?) REM REM 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-dll SETLOCAL if "%1"=="32" (set machine=x86) else (set machine=x64) set dll_file=%2 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_file=%dll_file_no_ext%.lib set 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 files del %exports_file% %def_file% %dll_file_no_ext%.exp 

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

like image 37
Conrad Poelman Avatar answered Sep 24 '22 16:09

Conrad Poelman