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

About these ads

26 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. 7 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. 8 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. 9 Stefan February 13, 2012 at 12:33 pm

    Thank you. Great blog…

    Stefan

  7. 10 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. 11 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. 12 raghunath April 21, 2012 at 10:03 am

    please send me how teate a dll in c

  10. 13 Cormac Phelan July 4, 2012 at 6:23 pm

    Excellent post, very useful

  11. 14 Sander September 28, 2012 at 1:43 pm

    Note that this does not work if functions are declared with __stdcall because the names are mangled then. You can then either change the calling convention (to __cdecl), manually mangle the names in the .def file (append @ where you can get from the linker errors) or create the .lib by using the stub method in the msdn article.

  12. 16 Alain October 2, 2012 at 10:17 am

    Hi! I just created the lib from my dll, but I have a doubt. Since my dll was 5MB and the lib is 4Kb I think that I have to use the dll together with the lib. Is it true?

    thank you for your post!

  13. 18 sparrow October 6, 2012 at 7:22 am

    I have followed your tutorial directly, but when I tried to create *.lib from *.def file, I got LNK1107 error invalid or corrupt file. Cannot read at 0x93E. I checked my def with hexeditor tool and it turns out that there is no char at this index – the last char is indexed under 0x93D. Have anyone experienced such problem?

    • 19 timoncze October 13, 2012 at 12:35 am

      It means that something is wrong in your DEF file. It read the whole thing, but is probably expecting something more (or less). Analyze it and try to fix it.

  14. 20 zibri October 13, 2012 at 9:48 pm

    It would be great to have an automated way to do the opposite…
    I have a .lib and a .h and I with to create a dll with all functions in the .lib.
    Since the lib is HUGE to write a wrapper would be a hard work.
    Also, I need to do this to use the DLL from managed code (vb net) using dllimport. Any clues?

  15. 21 Phyllis December 5, 2012 at 5:34 pm

    Fantastic goods from you, man. I have understand your stuff previous to and you’re just too wonderful. I really like what you’ve acquired
    here, really like what you’re saying and the way in which you say it. You make it entertaining and you still take care of to keep it wise. I cant wait to read far more from you. This is actually a wonderful website.

  16. 22 wordpress hosting January 9, 2014 at 9:30 pm

    Everything is very open with a precise description of the challenges.
    It was truly informative. Your site is extremely helpful.
    Many thanks for sharing!

  17. 23 mitzvah February 11, 2014 at 7:28 pm

    Hello, I do think your website could possibly be having web browser compatibility problems.
    Whenever I take a look at your blog in Safari, it looks fine however, if opening in I.E., it has some overlapping issues.
    I just wanted to provide you with a quick heads up!
    Besides that, wonderful website!

  18. 24 xbox repai February 25, 2014 at 9:49 pm

    I’ve learn some just right stuff here. Definitely price
    bookmarking for revisiting. I surprise how a lot attempt
    you put to create this kind of fantastic informative website.

  19. 25 top web hosting April 14, 2014 at 4:48 pm

    I am not sure where you’re getting your info, but good topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for magnificent information I was looking for this information for my
    mission.


  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 )

Google+ photo

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

Connecting to %s





Follow

Get every new post delivered to your Inbox.

%d bloggers like this: