How to convert a dynamic dll to static lib?

2020-02-06 18:34发布

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?

6条回答
唯我独甜
2楼-- · 2020-02-06 18:58

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.

查看更多
欢心
3楼-- · 2020-02-06 19:04

I agree with Jerry, and if it is a deployment problem, you may use Nullsoft Scriptable Install System.

查看更多
干净又极端
4楼-- · 2020-02-06 19:08

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

查看更多
倾城 Initia
5楼-- · 2020-02-06 19:11

False, it is possible to do this. For example there is a tool called dlltolib which can do it.

查看更多
可以哭但决不认输i
6楼-- · 2020-02-06 19:13

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.

查看更多
仙女界的扛把子
7楼-- · 2020-02-06 19:14

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.

查看更多
登录 后发表回答