LppEdd
LppEdd

Reputation: 21104

How Dynamic-link library works

I was thinking: when I produce a DLL with Visual Studio (C++) it generates

And I have a .h file

So, why not developing directly a static .lib library?
For example, why Office doesn't have .lib files?

And, in the future, if I change the DLL, will I have to send to all the machines also the new .lib file and .h file?

Upvotes: 4

Views: 1645

Answers (3)

Mats Petersson
Mats Petersson

Reputation: 129314

This is a SHORT version that tries to answer your specific questions. The subject of "how DLL's and shared libraries work" is quite a few pages long, and I'm just not going to write that all as an answer.

To start with the parts the compiler generats, a .lib and a .dll file, are used as follows: The .lib file contains "stubs" that go into .exe file using the .dll, and contain "where to find the different functions" in the .dll file. So this is used only when building the .exe file. Similarly, the .h file is used when compiling the .dll and .exe file, and aren't used once you have the final binaries (.exe and .dll files) that make up the product.

The purpose of using DLL's over static linking is most important when there are multiple executables that use the same .dll file. If there is only one "user" of a DLL then the the benefits are reduces, but there are still some benefits (such as compartmentalisation, ability to supply updates to only that part of the code, plugins, etc).

Assuming your .dll is part of a program that contains only binaries, no source code, then you only need to distribute the new .dll [as long as the functions haven't changed in such a way that the .exe or other .dll's that use this .dll has to change, of course].

Upvotes: 2

mkaes
mkaes

Reputation: 14119

The .lib file and the header file is the static part of your dynamic library.
You need the .lib and header file in order to compile and link a program so that it uses your library.

So why not use a static library?
There are multiple reasons. One would be that if you use a static library every change of your library causes a recompile of your program. Another that the size of your program will increase. And some more.

So for your second question the lib file are useless for a user of your program. In case of office. As long as you don't have the sources and a compiler the lib files will not help.

For your last question. The answer is also simple. No you don't need to distribute the .lib files. You can replace the dll files with new versions as long as the interface stays the same.

Upvotes: 3

Balog Pal
Balog Pal

Reputation: 17163

The .h and .lib is only for developers. Whoever writes a program to use the DLL.

Those who just execute the application need only the .DLL.

So if you release a new version, you send DLL to users and the triplet to developers. Unless you changed the public interface (the exports), the old clients will be happy to use the updated DLL without any work.

If you instead build a static .lib, every client must rebuild his binaries.

Upvotes: 4

Related Questions