Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?

What is inside of a .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?

How come there is no need for a .lib file in dynamically linked dynamic library and also that in static linking, the .lib file is nothing but a .obj file with all the methods. Is that correct?

like image 488
Sulla Avatar asked Jul 14 '10 20:07

Sulla


People also ask

Is .LIB a static library?

Examples of static libraries (libraries which are statically linked) are, . a files in Linux and . lib files in Windows.

Is .LIB static or dynamic?

A static library gets compiled into the client. A . lib is used at compile time and the contents of the library become part of the consuming executable. A dynamic library is loaded at runtime and not compiled into the client executable.

What does static library contain?

In the C programming language, a static library is a compiled object file containing all symbols required by the main program to operate (functions, variables etc.) as opposed to having to pull in separate entities. Static libraries aren't loaded by the compiler at run-time; only the executable file need be loaded.

What is statically linked and dynamically linked libraries?

Statically-linked files are 'locked' to the executable at link time so they never change. A dynamically linked file referenced by an executable can change just by replacing the file on the disk. This allows updates to functionality without having to re-link the code; the loader re-links every time you run it.


2 Answers

For a static library, the .lib file contains all the code and data for the library. The linker then identifies the bits it needs and puts them in the final executable.

For a dynamic library, the .lib file contains a list of the exported functions and data elements from the library, and information about which DLL they came from. When the linker builds the final executable then if any of the functions or data elements from the library are used then the linker adds a reference to the DLL (causing it to be automatically loaded by Windows), and adds entries to the executable's import table so that a call to the function is redirected into that DLL.

You don't need a .lib file to use a dynamic library, but without one you cannot treat functions from the DLL as normal functions in your code. Instead you must manually call LoadLibrary to load the DLL (and FreeLibrary when you're done), and GetProcAddress to obtain the address of the function or data item in the DLL. You must then cast the returned address to an appropriate pointer-to-function in order to use it.

like image 167
Anthony Williams Avatar answered Oct 12 '22 15:10

Anthony Williams


I found following answer from Hans also useful here.It clears the air that there could two types of lib files.

A LIB file is used to build your program, it only exists on your build machine and you don't ship it. There are two kinds. A static link library is a bag of .obj files, collected into a single file. The linker picks any chunks of code from the file when it needs to resolve an external identifier.

But more relevant to DLLs, a LIB file can also be an import library. It is then a simple small file that includes the name of the DLL and a list of all the functions exported by the DLL. You'll need to provide it to the linker when you build a program that uses the DLL so it knows that an external identifier is actually a function exported by the DLL. The linker uses the import library to add entries to the import table for the EXE. Which is then in turn used by Windows at runtime to figure out what DLLs need to be loaded to run the program.

like image 39
irsis Avatar answered Oct 12 '22 14:10

irsis