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

















Thanks for this post.
Amazing! Didn’t even know that this possible!
GREAT , GOD BLESS
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.
Thank you verry much ; it was verry helpfull for me.
Regards
Excellent, saved me a lot of time. Thanks very much.
So, do you use the decorated names or the undecorated names?