Static vs Dynamic

larry nevins
3 min readOct 19, 2021

--

Static vs Dynamic Libraries:

This essay is to introduce C libraries, both static and dynamic and answer these questions:

  • Why use libraries in general
  • How do they work
  • How to create them (Linux only)
  • How to use them (Linux only)
  • What are the differences between static and dynamic libraries
  • What are the advantages and drawbacks of each of them

We all know what a library is, a place where information is stored. Before we can dive deeper into libraries, we need to understand functions, these are our books that that help us perform a task. Every C function has main() function and functions that we can make. The best thing about functions is that we can reuse them.

Now we can talk about libraries, to reuse functions in our new programs, one can use libraries to share these functions easily.

There are two types of libraries, Static and Dynamic libraries.

Static libraries are linked with the program once compliation starts, meaning they are part of the executable file always. You can ensure all the functions are included with the executable,

Static libraries with lots of functions make the overall size of the executable file.

To make a static file for C, just use the gcc compiler. The first step is to create a directory with all the C files of all the required functions. In that directory, you need to type this command to start the process, gcc -Wall -pedantic -Werror -Wextra -c *.c

The -c flag turns the c files into o files. If you ls the files in your directory all the c. files would have a corresponding .o file.

Next, use the archiver program to turn o files into our static library, let’s create a library called “libmy.a”. Put this into the command line, ar -rc libmy.a *.o

The -c flag creates an archive and the -r flag inserts the files into the archive.

To view your static library contents, you’ll need to add the -t flag.

Ar -t libmy.a

Here is another program checking our libraries in the nm option. It lists each symbol’s value, type, and the name of the object file, just do nm libmy.a

Next how to compile it with our programs, for example, our program is named “main.c” and compile with with “libmy.a” library. The command is, gcc main.c -L. -lmy -o main

The -L option tells where it’s located in the library.

Voila here is your file named main that has the static library and all of the functions.

Dynamic Library

Dynamic libraries are like Static libraries. The major difference is the library does NOT get compiled into the executable. Instead, it is outside the executable file. One advantage is updating a function and distributing it instead of compiling the whole program. The dynamic library library will be smaller in file size.

Now let’s create a dynamic library!

First we need to input, gcc -Wall -pedantic -Werror -Wextra -c *.c -fPIC

Just like the static library, we use the -c option to make the compiler stop once it turns the files into object .o files. We have to use a -fPIC option which is short for “position independent code”. -fPIC is used because multiple programs can use one instance of the dynamic library. We can’t use fixed addresses to store them so it will make the assembly code relative.

The next step is using the gcc compiler so let’s call the dynamic library called “libmy.so” The command is: gcc *.o -shared -o libmy.so

The -shared is telling the compiler that we are making a dynamic library.

The last thing to do is export the path so that the program knows where to execute the code. Export using: export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

--

--