Generate a .lib from a DLL with Visual Studio

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

visual studio team system

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:vs_dumpbin
    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:
  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]




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


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


  • 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.

  • 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)
    $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 (“”);
    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

    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:
    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.

Leave a Reply

or Login (not required)

HTML tags allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>