Static and dynamic libraries

Paola Andrea Garcia Altamirano
5 min readSep 25, 2021

--

Libraries contain the object code of many programs that allow you to do common things, such as read the keyboard, write on the screen, handle numbers, perform mathematical functions, etc.

Libraries are classified by the type of work they do, there are input and output libraries, mathematical libraries, memory management libraries, text management libraries and as you can imagine there are many libraries

available and all of them have a specific function.

In C, certain types of files that we can import or include in our program are known as libraries. These files contain the specifications of different functionalities already built and usable that we can add to our program, such as reading from the keyboard or displaying something on the screen among many others.

When being able to include these libraries with definitions of different functionalities we will be able to save us a great amount of things, let’s imagine for example that every time we need to read by keyboard, we should then create a function that does it (something really complex), when being able to count on the libraries in C, we will be able to make use of a great variety of functions that will facilitate the life to us and will increase the modularity of our codes.

https://miro.medium.com/max/644/1*-uxhNbxHwrPlmCRGWOa0xw.jpeg

  • How do they work:

Libraries in C are files or blocks of code with special functions that we can include in our code by importing their functionalities. Many of the libraries that we are going to be using will be libraries that come by default in the C and C ++ compiler. The standard libraries for example, “stdlib.h” and “stdio.h”, which import functions and special data types, communication with input and output peripherals such as screen and keyboard as well as file handling.

  • How to create them:

Let us create and use a Static Library in UNIX or UNIX like OS.
1. Create a C file that contains functions in your library.

/* Filename: lib_mylib.c */

#include <stdio.h>

void fun(void)

{

printf("fun() called from a static library");

}

We have created only one file for simplicity. We can also create multiple files in a library.

2. Create a header file for the library

/* Filename: lib_mylib.h */

void fun(void);

3. Compile library files.

gcc -c lib_mylib.c -o lib_mylib.o

4. Create static library. This step is to bundle multiple object files in one static library (see ar for details). The output of this step is static library.

ar rcs lib_mylib.a lib_mylib.o

5. Now our static library is ready to use. At this point we could just copy lib_mylib.a somewhere else to use it. For demo purposes, let us keep the library in the current directory.

  • How to use them

1. Create a C file with main function

/* filename: driver.c */

#include "lib_mylib.h"

void main()

{

fun();

}

2. Compile the driver program.

gcc -c driver.c -o driver.o

3. Link the compiled driver program to the static library. Note that -L. is used to tell that the static library is in current folder (See this for details of -L and -l options).

gcc -o driver driver.o -L. -l_mylib

4. Run the driver program

./driver 
fun() called from a static library

Following are some important points about static libraries.
1. For a static library, the actual code is extracted from the library by the linker and used to build the final executable at the point you compile/build your application.

2. Each process gets its own copy of the code and data. Where as in case of dynamic libraries it is only code shared, data is specific to each process. For static libraries memory footprints are larger. For example, if all the window system tools were statically linked, several tens of megabytes of RAM would be wasted for a typical user, and the user would be slowed down by a lot of paging.

3. Since library code is connected at compile time, the final executable has no dependencies on the library at run time i.e. no additional run-time loading costs, it means that you don’t need to carry along a copy of the library that is being used and you have everything under your control and there is no dependency.

4. In static libraries, once everything is bundled into your application, you don’t have to worry that the client will have the right library (and version) available on their system.

5. One drawback of static libraries is, for any change(up-gradation) in the static libraries, you have to recompile the main program every time.

6. One major advantage of static libraries being preferred even now “is speed”. There will be no dynamic querying of symbols in static libraries. Many production line software use static libraries even today.

Dynamic linking and Dynamic Libraries Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory. Examples of Dynamic libraries (libraries which are linked at run-time) are, .so in Linux and .dll in Windows.

We will soon be covering more points on Dynamic Libraries and steps to create them. https://www.geeksforgeeks.org/static-vs-dynamic-libraries/

  • What are the differences between static and dynamic libraries

In programming, a library is a collection of pre-compiled pieces of code that can be reused in a program. Libraries simplify life for programmers, in that they provide reusable functions, routines, classes, data structures and so on
which they can be reused in the programs.

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. Historically, libraries could only be static.
They are usually faster than the shared libraries because a set of commonly used object files is put into a single library executable file. One can 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.

Shared Libraries :
Shared libraries are .so (or in Windows .dll, or in OS X .dylib) files.
These are linked dynamically simply including the address of the library (whereas static linking is a waste of space). 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.

--

--