Difference between static and dynamic libraries

Difference between static and dynamic libraries

Before we start looking at the differences between a dynamic and a static library, we need to know what a library is and what it is for.

What is a library and what is it for?

Library is simply a collection of functions which can be added to your application and the functions called as necessary, just like any other functions in the application. More precisely any object, not only functions can be stored in a library, but the vast majority of libraries only contain functions.

Libraries are used to reuse code and be much more efficient when coding.

How do libraries work?

There are two types of libraries, static and dynamic.

Static library : when you compile a source file you get an object file. Depending on your platform its extension may be .o or .obj or anything else. A static library is basically a collection of object files, kind of like a .zip file but probably not compressed. The linker, when trying to generate an executable tries to resolve the referenced symbols, i.e. locate in which object file (be it in a library or otherwise) they are defined and links them together. So, a static library may also contain an index of defined symbols in order to facilitate this. Exact implementation depends on the specific linker and library file format but the basic architecture is as mentioned.

Dynamic library:

On Linux, static libraries should end in “.a” and dynamic libraries should end in “.so”). Instead of actually compiling the function implementation into object code and including it in the executable file, in a dynamic library, the linker will simply include the address of the beginning of the function we’re trying to use in the executable file. This eliminates the size issue caused by static libraries because the final executable file will include only addresses instead of entire function implementations.

How can we create the libraries?

Static library :

Once we have our code, to obtain a static library we must perform the following steps:

  • Get the object files (.o) from all our sources (.c). For this they are compiled with cc -c source.c -o source.o. The -c option tells the compiler not to create an executable, but just an object file. Here I put the cc compiler, because it is the one I used for the example, but you can use gcc.
  • Create the library (.a). To do this, use the ar command with the following parameters: ar -rv libname.a source1.o source2.o ... The -r option tells the ar command to insert (or replace if they are already in) the files object in the library. The -v option is "verbose", so it displays information as you do things. Next we put all the object files that we want. ar is actually a much more generic command than all of this and can be used to package any type of file (not just object files). You also have options to see what files are inside, delete some of them, replace them, etc.

Example:

First, Strings.c is compiled to generate its object file:

gcc -c Strings.c -o Strings.o

Then the file (ar) is used to produce a static library (named libStrings.a) from the object file Strings.o.

ar rcs libStrings.a Strings.o

Note: The library must start with "lib" and have the suffix ".a".

Link statically:

With the static library, we can statically link main.o with the library.

The -L flag indicates (a non-standard) directory where the libraries can be found.

The -l flag indicates the name of the library. Note, that it assumes the library to start with lib and end with .o (so lib and .o must not be specified)

$ gcc bin/main.o -Lbin/static -ltq84 -o bin/statically-linked

The created executable bin/statically-linked is not dependend on any other object file or library. It can be distributed without the .a file or the .o files. It can be executed on the shell like so:

$ ./bin/statically-linked


Dinamic library:

  • Compile the sources, as before, to get the objects.
  • Create the library with the ld command. The options for this command would be ld -o liblibreria.so object1.o object2.o ... -shared. The option -o liblibreria.so indicates the name we want to give to the library. The -shared option tells you to create a library and not an executable (default option). object1.o, object2.o ... are the object files that we want to put in the library.

Example:

First, Strings.c is compiled to generate its object file:

$ gcc -c Strings.c -o Strings.o

Then we use the -share flag to create a dynamic library:

$ gcc -shared -o Stringlibrary.so Strings.o

Note: To create the library always use -share and the name indicated for the static library must have the suffix .so

How are libraries used?

Static Libraries:

After creating the archive, we have to use it in a program. This is done by adding the library’s name to the list of object file names given to the linker, using a special flag, normally '-l'. Here is an example:

gcc main.o -L. -lutil -o prog

This will create a program using an object file “main.o”, and any symbols it requires from the “util” static library. Note that we omitted the “lib” prefix and the “.a” suffix when mentioning the library on the link command. The linker attaches these parts back to the name of the library to create a name of a file to look for. Note also the usage of the '-L' flag - this flag tells the linker that libraries might be found in the given directory ('.', referring to the current directory), in addition to the standard locations where the compiler looks for system libraries.

Dynamic Libraries:

Using a shared library is done in two steps:

  1. Compile Time — here we need to tell the linker to scan the shared library while building the executable program, so it will be convinced that no symbols are missing. It will not really take the object files from the shared library and insert them into the program.
  2. Run Time — when we run the program, we need to tell the system’s dynamic loader (the process in charge of automatically loading and linking shared libraries into the running process) where to find our shared library.

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

gcc main.o -L. -lutil -o prog

The differences between dynamic and static libraries?

Static Libraries: 

A Static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.

This executable and the process of compiling it are both known as a static build of the program.

Static libraries are faster than the shared libraries because a set of commonly used object files is put into a single library executable file which enables you to build multiple executables without the need to recompile the file.

Because it is a single file to be built, use of link commands are simpler than shared library link commands, because you specify the name of the static library.

Dynamic Libraries :

Shared libraries are .so files.

These are linked dynamically simply including the address of the library. Dynamic linking links the libraries at the run-time. Thus, all the functions are in a special place in memory space, and every program can access them, without having multiple copies of them.

*The advantages and drawbacks of each of them?

Static libraries:

+ are faster than the shared libraries because a set of commonly used object files is put into a single library executable file which enables you to build multiple executables without the need to recompile the file.

(+) No dependencies

(-) Higher memory usage, as the OS can no longer use a shared copy of the library.

(-)If the library needs to be updated, your application needs to be rebuilt.

Dynamic libraries:

(+) Consumes disk space and memory only once.

(+) If a .so is already loaded, the executable may load faster because it is a smaller file

(-) If a .so is not already loaded, the executable’s remaining undefined symbols must be resolved at start-up.

(-) There is a chance to break executables without relinking.

I hope I can help you understand what a library is, what they are for, how to use them and what are the differences between, dynamic and static.


Carlos Araque for Holberton School






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

Carlos Araque的更多文章

社区洞察

其他会员也浏览了