DLL and LIB files - what and why?

148,164

Solution 1

There are static libraries (LIB) and dynamic libraries (DLL) - but note that .LIB files can be either static libraries (containing object files) or import libraries (containing symbols to allow the linker to link to a DLL).

Libraries are used because you may have code that you want to use in many programs. For example if you write a function that counts the number of characters in a string, that function will be useful in lots of programs. Once you get that function working correctly you don't want to have to recompile the code every time you use it, so you put the executable code for that function in a library, and the linker can extract and insert the compiled code into your program. Static libraries are sometimes called 'archives' for this reason.

Dynamic libraries take this one step further. It seems wasteful to have multiple copies of the library functions taking up space in each of the programs. Why can't they all share one copy of the function? This is what dynamic libraries are for. Rather than building the library code into your program when it is compiled, it can be run by mapping it into your program as it is loaded into memory. Multiple programs running at the same time that use the same functions can all share one copy, saving memory. In fact, you can load dynamic libraries only as needed, depending on the path through your code. No point in having the printer routines taking up memory if you aren't doing any printing. On the other hand, this means you have to have a copy of the dynamic library installed on every machine your program runs on. This creates its own set of problems.

As an example, almost every program written in 'C' will need functions from a library called the 'C runtime library, though few programs will need all of the functions. The C runtime comes in both static and dynamic versions, so you can determine which version your program uses depending on particular needs.

Solution 2

Another aspect is security (obfuscation). Once a piece of code is extracted from the main application and put in a "separated" Dynamic-Link Library, it is easier to attack, analyse (reverse-engineer) the code, since it has been isolated. When the same piece of code is kept in a LIB Library, it is part of the compiled (linked) target application, and this thus harder to isolate (differentiate) that piece of code from the rest of the target binaries.

Solution 3

One important reason for creating a DLL/LIB rather than just compiling the code into an executable is reuse and relocation. The average Java or .NET application (for example) will most likely use several 3rd party (or framework) libraries. It is much easier and faster to just compile against a pre-built library, rather than having to compile all of the 3rd party code into your application. Compiling your code into libraries also encourages good design practices, e.g. designing your classes to be used in different types of applications.

Solution 4

A DLL is a library of functions that are shared among other executable programs. Just look in your windows/system32 directory and you will find dozens of them. When your program creates a DLL it also normally creates a lib file so that the application *.exe program can resolve symbols that are declared in the DLL.

A .lib is a library of functions that are statically linked to a program -- they are NOT shared by other programs. Each program that links with a *.lib file has all the code in that file. If you have two programs A.exe and B.exe that link with C.lib then each A and B will both contain the code in C.lib.

How you create DLLs and libs depend on the compiler you use. Each compiler does it differently.

Solution 5

One other difference lies in the performance.

As the DLL is loaded at runtime by the .exe(s), the .exe(s) and the DLL work with shared memory concept and hence the performance is low relatively to static linking.

On the other hand, a .lib is code that is linked statically at compile time into every process that requests. Hence the .exe(s) will have single memory, thus increasing the performance of the process.

Share:
148,164

Related videos on Youtube

Xonara
Author by

Xonara

Updated on December 19, 2020

Comments

  • Xonara
    Xonara over 3 years

    I know very little about DLL's and LIB's other than that they contain vital code required for a program to run properly - libraries. But why do compilers generate them at all? Wouldn't it be easier to just include all the code in a single executable? And what's the difference between DLL's and LIB's?

  • Lumi
    Lumi almost 13 years
    Turns out that .LIB files can be either static libraries (containing object files) or import libraries (containing symbols to allow the linker to link to a DLL). I'm wondering why this is so.
  • mox
    mox almost 12 years
    good explanation! Code is shared and data is (per default) not shared between the application(s) consuming a Dll.
  • Martin
    Martin over 11 years
    The security aspect was new to me. Does the above reasoning hold true in the case of a C# app calling a native unmanaged C++ dll?
  • Nick Russler
    Nick Russler about 10 years
    But the LIB is isolated too, isn't it? So an Attacker could simply analyze the LIB. Or is it a common scenario that the LIB is not accessible to the public?
  • mox
    mox about 10 years
    the LIB is "isolated" too, as far as the compiler process is concerned, but once the linker as put the parts together, the LIB is part of the EXE and is cannot be differentiated from your own code.
  • itachi
    itachi over 4 years
    @Lumi: Good point. In terms of DLLs we have two types of linking. Implicit linking, when we have a .lib file provided by DLL creator along with appropriate headers; this .lib is merely a descriptor of the target DLL, it contains addresses, entry point, etc. but no code. This .lib must be passed to the linker. The second one is explicit linking when we use the DLL by manually loading it with LoadLibrary function. In this type we don't need that .lib file, but we must put a little effort to find DLL exports, their addresses, and call these functions through pointers.
  • platinoob_
    platinoob_ over 3 years
    excuse me, if I have the header(.h) and c(.c) files included in my project, are libraries necessery for the executable to compile and run? I am not saying about the pros and cons of this, just if it will be able to compile & run
  • Kyberias
    Kyberias over 2 years
    There is no performance impact between code run from a lib or from a DLL.

Related