Differences between static and dynamic libraries

Differences between static and dynamic libraries

Why using libraries in general

A library in C is a collection of objects files exposed for use and build other programs, so instead of re-write sections of code we bring back information that is already existing, this is where it comes to the concept of a library. In C exist two kinds of libraries the first ones are the static that allows us to link to the program and are not relevant during the runtime, and the other ones are called dynamic libraries those are preferable use when you run a lot of programs at the same time who are using the same library and you want to be more efficient if you want to know more about static libraries I recommend you to check the next article.

What are the differences between static and dynamic libraries?

No hay texto alternativo para esta imagen

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 are the advantages and drawbacks of each of them?

Static libraries are just collections of object files that are linked into the program during the linking phase of compilation and are not relevant during runtime. This last comment seems obvious, as we already know that object files are also used only during the linking phase, and are not required during runtime - only the program's executable file is needed in order to run the program.

The complex phase of dynamic loading makes launching the program slightly slower, but this is a very insignificant drawback, that is out-weighted by a great advantage - if a second program linked with the same shared library is executed, it can use the same copy of the shared library, thus saving a lot of memory. For example, the standard "C" library is normally a shared library and is used by all C programs. Yet, only one copy of the library is stored in memory at any given time. This means we can use far less memory to run our programs, and the executable files are much smaller, thus saving a lot of disk space as well.

However, there is one drawback to this arrangement. If we re-compile the dynamic library and try to run a second copy of our program with the new library, we'll soon get stuck - the dynamic loader will find that a copy of the library is already stored in memory, and thus will attach it to our program, and not load the new (modified) version from disk.

How do they work

Dynamic libraries (also called shared libraries) are linked into the program in two stages. First, during compile time, the linker verifies that all the symbols (again, functions, variables, and the like) required by the program, are either linked into the program or in one of its dynamic libraries. However, the object files from the dynamic library are not inserted into the executable file. Instead, when the program is started, a program in the system (called a dynamic loader) checks out which shared libraries were linked with the program, loads them to memory, and attaches them to the copy of the program in memory.

No hay texto alternativo para esta imagen


How to create them (Linux only)

How to generate a shared object: (Dynamically linked object library file.) Note that this is a two-step process.

  1. Compile for "Position Independent Code" (PIC): When the object files are generated, we have no idea where in memory they will be inserted in a program that will use them. Many different programs may use the same library, and each loads it into a different memory in address. We need that all jump calls and subroutine calls will use relative addresses, and not absolute addresses. Thus, we need to use a compiler flag that will cause this type of code to be generated.
gcc -fPIC -c file1.c file2.c file3.c

With this command, we will create a list of object files with the names of file1.o, file2.o and file3.o.

2. Library File Creation: Unlike a static library, a shared library is not an archive file. It has a format that is specific to the architecture for which it is being created. Thus, we need to use the compiler (either the compiler's driver or its linker) to generate the library and tell it that it should create a shared library, not a final program file.

fcc -shared lib_name.so file1.o file2.o file3.o

How to use them (Linux only)

The compilation part is easy. It is done almost the same as when linking with static libraries:

gcc main.o -L. -l_name -o prog

The linker will look for the file 'lib_name.so' (-l_name) in the current directory (-L.), and link it to the program, but will not place its object files inside the resulting executable file, 'prog'.

Bibliography:

https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html








Monica G

R&D Engineer @ Mavenir Systems | SIP | IMS | RCS | Masters in Embedded Systems @ Manipal University

1 年

very well articulated

回复

要查看或添加评论,请登录

Juan David Tuta Botero的更多文章

社区洞察

其他会员也浏览了