0

I'm try to build a dynamic dll library on on windows using visual studio, but there are two file generated, one is dll file, another is .lib file;

My knowledege of using dll library is privode it to linker, I don't know what is ther purpose of .lib file, it has the same file extension as static lib, and it definitely is not static lib;

Does visual studio generate a .lib file when create dynamic dll library at all situtation?

Do I need to use the .lib file in my application?

How do I do use the .lib file in my application?

Liu Hao
  • 472
  • 4
  • 24
  • In `msvc` the `.lib` file created during the building of the dll is an import library not a static library. You link to the import library and use the dll at runtime. You don't use the dll to link. – drescherjm Nov 07 '21 at 14:39
  • 3
    The lib is used for linking when building (static linking) — it contains the hooks the dynamic linker will use later. The other is used when the program is loaded to run (dynamic linking). – wcochran Nov 07 '21 at 14:40
  • 1
    ***My knowledege of using dll library is privode it to linker*** For standard / unmanaged c++ (not using .net / cli) and msvc this is wrong. You don't link to dlls in native c++ when using msvc. You link to the import library for the dll. – drescherjm Nov 07 '21 at 14:46
  • Does this answer your question? [What is the use of .exp and what is the difference between .lib and .dll?](https://stackoverflow.com/questions/2727020/what-is-the-use-of-exp-and-what-is-the-difference-between-lib-and-dll) Or perhaps [What is .lib file contain while generating DLL](https://stackoverflow.com/questions/27616678/what-is-lib-file-contain-while-generating-dll) – JaMiT Nov 07 '21 at 16:54
  • @JaMiT the second question answer my question. since jwezorek has make the good answer too, I have accepted his answer – Liu Hao Nov 07 '21 at 17:17

1 Answers1

3

It has to do with the difference between "implicit" linking and "explicit" linking. The one sentence answer to your question is that that .lib file, often called a "stub lib" and officially called an "import library", is necessary when you do implicit linking but not otherwise.

In implicit linking, at compile time the compiler generates external function references for calls into the DLL, then the linker needs to link those to something: it uses the import library to help it do that. The linker treats these calls as special cases; it inserts code in the executable file for finding the DLL and for calling into the DLL for specific functions.

From the point-of-view of the programmer, implicit linking behaves like static linking except the DLL needs to be somewhere where the system can find it when the application is run or the system will pop up an error dialog at application startup.

Explicit linking means that it is the programmer's responsibility to find and load the DLL and to know what functions are in the DLL and how to call into them. See LoadLibrary and GetProcAddress for details. Explicit linking is useful if usage of a library is conditional or for applications with plugin architectures in which the actual DLLs to be used may not even be known at compile time.

In general though, if implicit linking does what you need, it is easier to just do it that way.

jwezorek
  • 8,592
  • 1
  • 29
  • 46