Dynamic + Static Libraries Explained

Dynamic + Static Libraries Explained

What are libraries in C?

A C library is simply a set of functions bundled up into a collection, so we can use them later. C libraries come in two varieties; static and dynamic.

Static libraries

Static libraries which have been around since the inception of C are like .zip files. They contain a variety of object files ( .o files) containing functions with a table of contents. A ‘librarian’ utility such as ar collects these object files which creates a statically linked library of executables.

Static libraries are part of the build environment, meaning that object files are bound to the executable file before execution This means that only the executable file is required in order for the program to run.

Some of the drawbacks with static libraries I mentioned in this post include issues dealing with file size and efficiency. Since all object files within a static library are linked to a program during compilation, the resulting file can grow to a large size — we may have linked functions that are not used by the program. If the library code is updated , the programs that use a particular library must be recompiled into a new executable.

Additionally, programs with large executables suffer from slow execution times and larger memtory footprints.

Dynamic libraries

A newer approach to libraries is the dynamic library. Object files can be linked only when they’re actually needed during the execution phase. Let’s explore how to create a dynamically linked library:

STEP 1: Creating object files

Well start off with preparing our .c functions in a directory:

Next, when we have all of our .c files corralled, it is time to create object code.

We’ll use the gcc -fPIC -c *.c command to compile our code into object files. gcc stands for the GNU Compiler Collection which is a compiler system for C.

Whenever a dynamic library is loaded, the loader modifies some addresses in the code depending on where the object was loaded. Code that is built into dynamic libraries are position-independent code so that the shared library can be readily loaded into any address in memory.

The -c option tells the compiler to assemble the .o files, but not to link them.

*.c includes all .c files in the current working directory.

Now we have all of our object files ready to be linked:

STEP 2:

With all of our object files ready, we are now ready to create our dynamic library.

In our directory where the .o files are located, we will run the following command:

gcc -shared -o spencerslibrary.so *.o -lc

The shared option tells the compile to produce an ‘object which can be linked with other objects to form an executable’ — or simply, a dynamic library.

The -o flag allows us the name our library. In this case, I named it spencerslibrary.so.

The -l(library) flag searches the the named library when linking. If more than one library is listed, the libraries are searched in the order in which they are found on the command line.

We can now test our dynamic library.

Step 3: Using our library

Let’s create a main file to test our code:

In order to enable our dynamic library, we’ll have to add it to our environment. LD_LIBRARY_PATH is used by our program to search for directories containing the libraries after it has been successfully compiled and linked and is searched when the program starts.

To set our environmental variable, we will execute the following command in bash:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

We can now run a simple command to test our code:

The length of “Dynamic libraries are fun” is 26!

Follow me on:

Github: https://github.com/spencerhcheng

Twitter: @spencerhcheng



To view or add a comment, sign in

Explore content categories