309

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?

1

5 Answers 5

408

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.

4
  • 95
    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.
    – Lumi
    Commented Jun 21, 2011 at 7:19
  • 3
    good explanation! Code is shared and data is (per default) not shared between the application(s) consuming a Dll.
    – mox
    Commented Jul 3, 2012 at 5:34
  • 13
    @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.
    – itachi
    Commented Nov 9, 2019 at 13:12
  • 1
    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
    – platinoob_
    Commented Dec 24, 2020 at 10:32
46

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.

3
  • 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?
    – Martin
    Commented Dec 5, 2012 at 5:13
  • 1
    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? Commented Mar 1, 2014 at 14:02
  • 10
    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.
    – mox
    Commented Mar 2, 2014 at 11:17
21

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.

12

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.

6

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.

2
  • 1
    There is no performance impact between code run from a lib or from a DLL.
    – Kyberias
    Commented Sep 28, 2021 at 6:03
  • 1
    This answer had me worried there for a second, but above comment is correct. As following SO answer states, the performance difference (if any) is negligible: stackoverflow.com/a/4385050/3735890
    – MDragon
    Commented Jun 23, 2022 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.