Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a static library and a dynamic one [duplicate]

Tags:

c

dynamic

static

In C language what is the difference between a static libraray and a dynamic library?

like image 801
KaramJaber Avatar asked Nov 26 '13 22:11

KaramJaber


People also ask

What is the difference between static and dynamic library?

What are the differences between static and dynamic libraries? Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.

What is static and dynamic library in C++?

A static library (or archive) contains code that is linked to users' programs at compile time. The executable file generated keeps its own copy of the library code. A dynamic library (or shared library) contains code designed to be shared by multiple programs. The content in the library is loaded to memory at runtime.

Are static libraries faster than dynamic?

Static linking produces a larger executable file than dynamic linking because it has to compile all of the library code directly into the executable. The benefit is a reduction in overhead from no longer having to call functions from a library, and anywhere from somewhat to noticeably faster load times.

What is difference between static and dynamic linking?

Definition. Static linking is the process of copying all library modules used in the program into the final executable image. In contrast, dynamic linking is the process of loading the external shared libraries into the program and then binds those shared libraries dynamically to the program.


2 Answers

This concept might be a little bit too broad to explain, but i will try to give you a basic idea from which you can study further.

Firstly, you need to know what a library is. Basically, a library is a collection of functions. You may have noticed that we are using functions which are not defined in our code, or in that particular file. To have access to them, we include a header file, that contains declarations of those functions. After compile, there is a process called linking, that links those function declarations with their definitions, which are in another file. The result of this is the actual executable file.

Now, the linking as I described it is a static linking. This means that every executable file contains in it every library (collection of functions) that it needs. This is a waste of space, as there are many programs that may need the same functions. In this case, in memory there would be more copies of the same function. Dynamic linking prevents this, by linking at the run-time, not at the compile time. This means that all the functions are in a special memory space and every program can access them, without having multiple copies of them. This reduces the amount of memory required.

As I mentioned at the beginning of my answer, this is a very simplified summary to give you a basic understanding. I strongly suggest you study more on this topic.

like image 97
Paul92 Avatar answered Sep 21 '22 19:09

Paul92


In windows:

The static library is a .lib file that will be linked inside your executable and won't change with time.

The dynamic library is a .dll file linked to your executable and may change depending on the dll file you load when you execute it.

like image 32
fernando.reyes Avatar answered Sep 19 '22 19:09

fernando.reyes