.lib
created with the .dll
is called the "import library" and it allows you to use the dll functions, including their header, as if they were statically linked in your executable file. It ensures that when the linker needs to fix the dll function addresses that reference the object files, it can find them inside the import library. Such functions found in the import library are actually stubs that extract the actual address of the corresponding function in the loaded dll from the import address table and jump directly to it (traditionally, now there is some ingenuity in the linker that avoids this double Jump).
The import library, in turn, contains special instructions for the linker, which instruct it to generate the corresponding entries in the import table of the executable file, which, in turn, is read at boot by the loader ("dynamic linker", in Unix terms). This ensures that the referenced DLLs are loaded before the entry point of your executable is called, and the IAT contains the correct addresses of the referenced functions.
Note that all this is basically just a convenience, allowing you to call the dll functions as if they were statically linked to your executable. You do not need a .lib file if you explicitly handle the dynamic load / function search (using LoadLibrary and GetProcAddress); it’s just more convenient to delegate all these things to the linker and the loader.
source share