Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is usefulness of .lib file generated while compiling the dll projects, can i use it for static linking? [duplicate]

I want to ask the usefullness of .lib file which is generated while compiling a dll project.

When we compile our projects, following files get generated: .dll .exp .lib .pdb

Now as we also have .lib file, can we use this file to statically link it to any other project. If not, then what is the use of this .lib file getting generated.

like image 358
Sweekritee Singh Avatar asked Feb 01 '17 06:02

Sweekritee Singh


1 Answers

The .lib generated along with a .dll is called "import library", and it allows you to use the dll functions including their header as if they were statically linked in your executable. It makes sure that, when the linker has to fix up the dll function addresses referenced into object files, it can find them inside the import library. Such functions found into the import library are actually stubs which retrieve the actual address of the corresponding function in the loaded dll from the Import Address Table and jump straight to it (traditionally; now there is some smartness in the linker that allows to avoid this double jump).

The import library, in turn, contains special instructions for the linker that instruct it to generate the relevant entries into the import table of the executable, which in turn is read at load time by the loader (the "dynamic linker", in Unix terms). This makes sure that, before the entry point of your executable is called, the referenced dlls are loaded and the IAT contains the correct addresses of the referenced functions.

Notice that all of this is mostly just convenience stuff to allow you to call dll functions as if they were statically linked into your executable. You don't strictly need the .lib file if you handle the dynamic load/function address retrieval explicitly (using LoadLibrary and GetProcAddress); it's just more convenient to delegate all this stuff to the linker and the loader.

like image 91
Matteo Italia Avatar answered Oct 09 '22 11:10

Matteo Italia