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

  • Twitter
  • Digg
  • del.icio.us
  • Propeller
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Mixx
  • Yahoo! Buzz
  • NewsVine
  • Facebook
  • Google Bookmarks

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


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>