# Generate a .lib from a DLL with Visual Studio

/* Posted January 21st, 2009 at 7:43am    */
/* Filed under C#, C/C++, Microsoft, Programming    */

Oftentimes programmers have to deal with external third party DLLs that their software rely on to function. This is both smart and useful because as long as the DLL interface remains the same, externally linked DLLs can be easily swapped in and out without having to recompile and rebuild code. However, linking code in Visual Studio with an external library requires that a .lib file exist – the DLL is actually not sufficient for this task. Fortunately, there is a way to generate a .lib from a DLL with Visual Studio. Creating the .lib file is a bit like reverse engineering the DLL’s symbols.

1. Regardless of your Visual Studio version, whether it’s for 2003, 2005, or 2008 .NET, open up the Visual Studio Tools command promopt (something like Start -> Program Files -> Microsoft Visual Studio -> Visual Studio Tools -> Visual Studio Command Prompt).
2. If you have a .def file for your DLL, you can skip this step. Otherwise if your DLL did not ship with a .def file, you have a bit of work ahead of you. You cannot generate a .lib without a .def file first, sorry. To generate a .def file from a DLL, execute the dumpbin command to extract the function names from your DLL. In our example, we will be building from sqlite3.dll.
dumpbin /exports C:\path\to\sqlite3.dllThe output appears below:
The function names boxed in red are what you need to care about. Copy just the function name text into a new file with .def extension. Start the new file with "EXPORTS” without the quotes and then have each function on its own line like this:
EXPORTS sqlite3_aggregate_context sqlite3_aggregate_count sqlite3_auto_extension sqlite3_bind_blob sqlite3_bind_double sqlite3_bind_int sqlite3_bind_int64 ...
3. With your new definition file in tow, in the command prompt you can execute the lib command to finally generate the .lib file:lib /def:C:\path\to\sqlite3.def /out:C:\path\to\sqlite3.lib /machine:x86
This generates the .lib file you can use to link with in your project file. Note that the /machine argument can take any number of machine configuration so be sure to choose the correct one. To get a list of all machine types, simply type “lib” by itself. Here is the usage below for your edification:
usage: LIB [options] [files] options: /DEF[:filename] /EXPORT:symbol /EXTRACT:membername /INCLUDE:symbol /LIBPATH:dir /LIST[:filename] 

/MACHINE:{AM33|ARM|EBC|IA64|M32R|MIPS|MIPS16|MIPSFPU|MIPSFPU16|MIPSR41XX| SH3|SH3DSP|SH4|SH5|THUMB|X86} /NAME:filename /NODEFAULTLIB[:library] /NOLOGO /OUT:filename /REMOVE:membername /SUBSYSTEM:{CONSOLE|EFI_APPLICATION|EFI_BOOT_SERVICE_DRIVER| EFI_ROM|EFI_RUNTIME_DRIVER|NATIVE|POSIX|WINDOWS| WINDOWSCE}[,#[.##]] /VERBOSE

And if you came looking for sqlite3.lib we just saved you a lot of time.

Tags:

### 14 Responses to “Generate a .lib from a DLL with Visual Studio”

• Comment from sascha

Thanks for this post.
Amazing! Didn’t even know that this possible!

• Comment from Bilal Ahsan

GREAT , GOD BLESS

• Comment from Dan

Lol. Yes, I was looking for how to generate sqlite3.lib so I couldn’t help but chuckle to myself when I saw that you used SQLite for your example. Thanks for the help, mate.

• Comment from Habiba

Thank you verry much ; it was verry helpfull for me.
Regards

• Comment from Alec

Excellent, saved me a lot of time. Thanks very much.

• Comment from Carl C

So, do you use the decorated names or the undecorated names?

• Comment from Harish Surana

Thanks for sharing such a wonderful information

• Comment from Ramesh Marikhu

Thanks a lot. This was really very helpful. The creation of .def file was a bit tricky as my dll output a lot of spurious functions preceded with an underscore. I did not include those functions in the .def file and it all worked fine.

• Comment from Anton Tyutin

I can recommend this simple recipe to all R users who write R extensions under Windows and need file R.lib, which is missing from the official R distribution. I have successfully used this recipe to generate import library R.lib from dynamic library R.dll. Thank you very much.

• Comment from przybyle

seriously fortesdotcom u do all that way 2 fast, u type 2 fast, how 2 learn something from that, and the resolution of the vid is poor, u might can be a good tutor but u realy need 2 slow down…

• Comment from aleksas

wrote a Powershell script to automate dll->lib generation:

$pattern = “\s+[A-Z0-9]+\s+[A-Z0-9]+\s+[A-Z0-9]{8} (?.*)”;$platform = “x86″;

if($args.length > 0) { if($args[1])
{
$platform =$args[1];
}

$dll = [System.IO.Path]::GetFilename($args[0]);
$def = [System.IO.Path]::ChangeExtension($dll, “def”);
$lib = [System.IO.Path]::ChangeExtension($dll, “lib”);

Write-Host (“Generating ” + $def + ” file…”); “EXPORTS” >$def;
&”dumpbin” “/exports” $args[0] | select-string$pattern | %{$null =$_.Line -match $pattern; (“t” +$matches['symbol']) >> $def; } Write-Host (“Generating ” +$lib + ” file…”);
&”lib” (“/def:” + $def) (“/out:” +$lib) (“/machine:” + $platform) | out-null; Write-Host (“”); } else { Write-Host “Start powershell from VisualStudio commandline then use this script.”; Write-Host “Script takes two parameters: dll filename and platform.”; Write-Host “example: .\GenerateLibFromDll.ps1 hello.dll ”x64”"; Write-Host “or to process all dlls in the dir: gci *.dll | foreach {&”.\GenerateLibFromDll.ps1”$_.Name ”x64`”}”;;
}

• Comment from Anonymous

Hello,
Sorry to mention, but this only generates a “import library”, which still forces you to have the “dll” in proper search path.
I’m looking for solution which would transfer executable code from dll to lib (not only to generate lib which would still link me to the dll), but only found a 999\$ tool for now…

• Comment from Sergey

It is always worth to mention the source, MSDN in this case: http://support.microsoft.com/?scid=kb%3Ben-us%3B131313&x=1&y=15.
But thank you, anyway.

• Comment from Serhiy Ivanov

Bad article because incomplete. It works only for _cdecl-type call. For otheres you have to have header and create dummy cpp based on this header as microsoft suggests by Sergey link before.