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
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
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.
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
To output in a file, use the “>” character followed by the filename :
dumpbin /exports C:\yourpath\yourlib.dll > c:\yourlib.txt
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?
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
Thank you. Great blog…
Stefan
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.
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.
please send me how teate a dll in c