Create .lib file from .dll

When working with 3rd party win dll’s you somtimes miss the according .lib file required to compile against it. There is a MS KB article showing how to generate a .lib file from a .dll, however the required steps are not described detailed enough I think. So here is my quick guide:

Open the Visual Studio Command Prompt, you find its shortcut in Start->Programs->Microsoft Visual Studio->Tools. Now run the dumpbin command to get a list of all exported functions of your dll:


dumpbin /exports C:\yourpath\yourlib.dll

This will print quite a bit of text to the console. However we are only interested in the functions:


ordinal hint RVA      name

1    0 00017770 jcopy_block_row
2    1 00017710 jcopy_sample_rows
3    2 000176C0 jdiv_round_up
4    3 000156D0 jinit_1pass_quantizer
5    4 00016D90 jinit_2pass_quantizer
6    5 00005750 jinit_c_coef_controller
...etc

Now copy all those function names (only the names!) and paste them into a new textfile. Name the nextfile yourlib.def and put the line “EXPORTS” at its top. My yourlib.def file looks like this:


EXPORTS
jcopy_block_row
jcopy_sample_rows
jdiv_round_up
jinit_1pass_quantizer
jinit_2pass_quantizer
jinit_c_coef_controller
...

Now from that definition file, we can finally create the .lib file. We use the “lib” tool for this, so run this command in your Visual Studio Command Prompt:


lib /def:C:\mypath\mylib.def /OUT:C:\mypath\mylib.lib

That’s it, happy coding :)

11 Responses to “Create .lib file from .dll”


  1. 1 Mark Bosold March 26, 2009 at 4:33 pm

    Thanks so much for your concise explanation of creating a .lib file. I dislike going to MSDN to find out how to solve simple problems. I find I must read and digest a lot of information before I can apply it to my immediate problem. Its sites like yours that make development fun! Thanks again, Mark

  2. 2 Balaji Badam July 13, 2009 at 6:14 am

    Totally agree with the posting above, Thank you very much for the simple instructions. Really save me a lot of time. I do not know why msdn cannot be as simple as that.

  3. 3 Abhishek Dey June 20, 2010 at 8:11 am

    Hi,

    Thanks for this great Article. One last thing, How can i redirect the dumpbin’s output directly to a file instead of copying each function name ?

    Thanks and Regards,

    ABHISHEK DEY

    • 4 Raymond February 21, 2011 at 2:26 pm

      To output in a file, use the “>” character followed by the filename :

      dumpbin /exports C:\yourpath\yourlib.dll > c:\yourlib.txt

  4. 5 ramesh prasad April 19, 2011 at 7:28 am

    Hi I followed the same and also the .lib file created successfully. But when I am trying to use the .lib file in vc++ project and calling the function, I am getting linker error. Can you plz tell me what could be the problem?

  5. 6 Pat October 18, 2011 at 8:08 am

    Thanks for these quick and easy instructions… it worked without much trouble for me. One problem I did run into was that the dll I was interfacing was written in Borland C++. Apparently, the _ prefix convention differs between this and Visual Studio. All the functions in the dll were already prefixed by an underscore, and when creating the lib for Visual Studio (using the above instructions), it then gave them another underscore. This caused my program to fail to link using the lib. I believe that is what they’re referring to in the MSDN article: “The reason for this limitation is based on an assumption made by the LIB utility that all names are automatically exported without a leading underscore. This is only true for _cdecl function names.”

    To fix it, I put an underscore at the beginning of each dll function prototype and calls to the functions in my c++ code (which previously worked when compiled with Borland C++). After doing that, the conventions matched, and everything linked properly. I probably could have tried following the “Stubbing Out Functions” instructions, but this worked with minimal effort, so I’m not gonna mess w/ it. :-) You can’t just remove the _ from the function names in the def file, since the dll won’t find the functions without the _.

    Hope this helps in case anyone runs into a similar problem.

    Pat

  6. 7 Stefan February 13, 2012 at 12:33 pm

    Thank you. Great blog…

    Stefan

  7. 8 Herbert Stocker April 19, 2012 at 9:20 am

    If you cannot use dumpbin, e.g. because it crashes, like in my case, then you can use Dependency Walker for getting the symbol names:
    1.) drop your DLL into Depnedency Walker (it may help to use 32 bit Dependency Walker for 32 bit DLLs and 64 bit DepWalker for 64 bit DLLs.)
    2.) Click the pane with the export names.
    3.) Press Ctrl+A Ctrl+C or do right-click -> Select All, right-click -> Copy Function Names
    4.) paste them into your .def file and add the word EXPORTS on the top.

  8. 9 Jeff Ryan April 20, 2012 at 9:53 pm

    Thanks for the post. Saved my day.

    Just to help future readers, lib /MACHINE: is another option that can possibly help resolve symbol mismatches when you are using 32bit and 64bit builds.

  9. 10 raghunath April 21, 2012 at 10:03 am

    please send me how teate a dll in c


  1. 1 Unresolved external symbol caused by name mangling using Visual Studio 2010 Trackback on February 1, 2012 at 12:02 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s





Follow

Get every new post delivered to your Inbox.