I write a program helloworld.exe; it depends on a.dll. I don't have the source code of the a.dll, which is a dynamic dll. How can i change it to static library, so I can link it into helloworld.exe?
7 Answers
Sorry, but there's no direct way to do so. A DLL is a fully linked executable format file, where a static library is a collection of separate object files collected together. With a little bit of work, you can convert a static library to a DLL, but doing the reverse is non-trivial (to put it mildly).

- 476,176
- 80
- 629
- 1,111
-
1+1 for the only correct answer to the OP's original question. (My answer is just a workaround) – Billy ONeal Apr 05 '10 at 04:32
-
Not just that though, for dll's which arent executables but rather are as they sound: "a dynamically linked library" there are tools to take the .lib and .dll and convert it into a .a (static lib). I've even gotten away with (on a couple of occasions) simply renaming (yes, i have no idea why in the world this worked) and linking against the library and having it work (although, libraries it depended on were still dynamically loaded) – chacham15 Aug 24 '11 at 06:49
As Jerry said, you cannot do it directly. You can, however, package your program into something like a self extracting RAR file which includes the DLL as part of the single EXE, which automatically extracts the EXE and associated DLLs to a temp folder and starts the main program.

- 104,103
- 58
- 317
- 552
False, it is possible to do this. For example there is a tool called dlltolib which can do it.

- 13,719
- 26
- 104
- 207
-
2In fact it quite clearly states in the description that it only creates an import library: "A .NET command-line app that produces an import library (.lib) from a target dynamic link library (.dll)." – Joe Sep 02 '16 at 08:10
-
3@Joe why do you assume that that is the software that I was talking about and downvote? A quick google search would have showed you http://www.binary-soft.com/dll2lib/dll2lib.htm which states on the first line "Convert DLL file into its equivalent static library." – chacham15 Sep 09 '16 at 15:37
-
2"a quick google search" lead me to the same Github project as Mike. The comment is also about the software mentioned by Mike. The downvote is because your answer is not very clear, especially as it didn't include a link under which to find the tool. But you changed that now, so I'll undownvote. – Joe Sep 09 '16 at 16:59
-
1In any case, the linked tool doesn't seem too useful as it is 32-bit only which is quite limiting nowadays. – Joe Sep 09 '16 at 17:00
-
1Also, the single user developer license is now $100 as of 09/08/2019 for binary-soft's dll2lib tool. – BugSquasher Sep 09 '19 at 04:49
-
@КонстантинВан no. that tool actually converts the dll and there is no import library. – chacham15 Jan 15 '21 at 03:27
Perhaps what is being asked is how to create a .LIB file that will permit statically linking to the .DLL. Here are sample commands to achieve this:
set DUMPBIN="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx86\x86\dumpbin.exe"
set LIBX="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx86\x86\lib.exe"
set IN="\MyFolder\MyDll.dll"
set DEF="\MyFolder\MyDll.def"
set LIB="\MyFolder\MyDll.lib"
:: [1] run dumpbin
%DUMPBIN% /EXPORTS /NOLOGO /OUT:%DEF% %IN%
:: [2] edit .DEF file to look proper
:: [3] run LIB
%LIBX% /DEF:%DEF% /OUT:%LIB%

- 4,114
- 2
- 34
- 39
I agree with Jerry, and if it is a deployment problem, you may use Nullsoft Scriptable Install System.

- 1,902
- 2
- 18
- 28
On windows, you can get the lib file to run your program if you have the corresponding def file. You can use the command prompt window of visual studio to get the lib file. The command line is as follows: lib /def:XXX.def /machine:x64 (or x86 to get 32bit lib)/out:XXX.lib. You need to make sure the def file and dll file are in the same folder and you have changed the directory to the folder.

- 27
- 5
-
2Aaaaand, [that’s an _import library_](https://stackoverflow.com/questions/9946322/how-to-generate-an-import-library-lib-file-from-a-dll), which is a static **loader** for the DLL. – Константин Ван Jan 11 '21 at 07:18
I am absolutely horrified at the lack of understanding in at least one of the (supposedly popular?) answers.
I have written a linker from the ground up, every line of code. I know everything there is to know about DLLs.
A DLL contains much much more information than a lib, and it does, guaranteed, contain absolutely everything that a lib contains. Every last item.
To convert a dll to a lib, you can follow the simple steps provided in the following well-written article.
https://adrianhenke.wordpress.com/2008/12/05/create-lib-file-from-dll/
(I am not adrian henke, just for information)
The process described allows you to create a lib directly from a dll, without requiring the def file. In fact, it actually allows you to create the def file from the dll, since the dll contains all of this information.
I can guarantee that it works perfectly, since I actually ran the exact same process on a dll and checked the result. The lib is correct, and will allow you to link.
Oh and by the way, it is utterly, completely, totally impossible to convert a lib to a dll.

- 21
- 1
-
1Don't know what's with the downvotes, but your link is 404. adrianhenke.wordpress.com has been deleted by the user – BugSquasher Sep 09 '19 at 04:51
-
5This, like some other answers, seems like it will just create the import .lib, which will still require the .dll in runtime. A real static .lib would contain the code also. – jpa Mar 29 '20 at 05:19
-
1No need for the attitude! Here is an [archived copied](https://web.archive.org/web/20140219172454/https://adrianhenke.wordpress.com/2008/12/05/create-lib-file-from-dll/) of the now-dead link. As jpa said, that link only says how to make an import library, not a full .lib containing the code. Like you, I don't obviously see what's missing from a .dll that would make it impossible to convert to a lib, except that arranging to call its DllMain would take a bit of effort, but I think you're being a bit over confident in your claims, especially since you haven't presented an actual solution. – Arthur Tacca Jun 23 '20 at 18:04
-
I wonder why so many negatives, I guess its because you didn't give something easy to CTRL-C + CTRL-V. Its entirely possible, I just disassemble a DLL, it gives me ".s" and then "compile" it again to a ".a". Easily done with standard tooling. – Luiz Felipe Jun 27 '20 at 16:08