Dynamic Library = Flexible Library

Colton Walker
4 min readJan 15, 2017

--

You may be thinking, “What’s a library?”

A library is a powerful construct of programming. It involves a collection of code that typically has reoccurring usage value to the programmer and is stored in a separate file to be called on when needed in a main executable file.

Lets briefly talk about the available libraries, static and dynamic. A static library can usually be identified on linux with having a archive or “.a” extension. Static libraries are hard linked to the final executable file. The cons of using static is it takes up memory and is harder to make changes to it as it is apart of the executable and would need to be re-linked after changes are made. You can read more on Static Libraries here. Dynamic libraries on the other hand behave a bit differently. They are considered to be more flexible in regards to making edits and sharing them because they are loaded into the program at runtime and are not made into the actual executable file. You can spot a dynamic library on a linux machine by its “.so” extension, which indicates that its a shared object. In the compilation process:

  1. Preprocessor — Lines starting with ‘#’ are interpreted in this process and passed as simple macros. Also, and any comments which are only for human eyes are stripped.
  2. Compilation — In this stage, the code is now ready to be turned into assembly language, which in its form is still somewhat readable.
  3. Assembly — Assembly code is then turned into object code (a.k.a machine code).
  4. Linking — during this final stage, pieces of code that are missing are finally linked to the object before turning it into an executable. “What could be missing?” Items like header files or libraries needed to complete the code.

In step 4 of compilation is where dynamic libraries, do their job.

Now, the part I suspect you’ve been waiting for, lets check out some keystrokes to making a dynamic library and see if we can get a tune out of this old trombone.

Step 1 — We’ll first need to compile all source files into an object file counterpart.

.c -> .o

gcc -c -Wall -Werror -fpic yourfile.c

Aside from the normal gcc arguments, you might have noticed “-fpic”. This creates position-independent code and accesses constants addresses via a global offset table. It comes with size limitations and if you find that applies to you, use “-fPIC” instead which has a larger upper limit.

Step 2— Next, we’ll take our newly created object files and make a dynamic library.

.0->lib.so

gcc -shared -o libyourelibraryname.so yourfile.o

Note: By convention, your library should start with the “lib” and end with a “.so” extension.

Step 3 — Now that we have a shared library, we need to link it to

gcc -Wall -o omega main.c -lyourelibraryname

Note: omega is our final program and -lyourelibraryname is used to search for your “.so” libyourelibraryname.so.

Step 4— Almost done, so be excited! We have just one more step. If you attempt to run “./omega” you will be greeted with an error. This happens because, the loader doesn't know where to look. So lets fix that with the use of the environment variable LD_LIBRARY_PATH.

If you wish to check out what value that current variable holds just type:

echo $LD_LIBRARY_PATH

Chances are you’ll get nothing in return because this variable is reset periodically. To set the variable you’ll need your working path which can be gained by typing “pwd” (print working directory)into the terminal. For this example, lets use “/home/myusername/currentdirectory/” as our working directory

LD_LIBRARY_PATH=/home/myusername/currentdirectory/:$LD_LIBRARY_PATH

and finally, exporting the environment changes is needed to solidify the changes. Simply repeat the previous keystroke with “export” in front of it

export LD_LIBRARY_PATH=/home/myusername/currentdirectory/:$LD_LIBRARY_PATH

now lets try and re-run our program

./omega

and Yahtzee!

You’ll find using dynamic, or shared libraries are most useful when having a library that is large in size and may go through different iterations. Although, because it has more moving parts, you may run into capability issues. Using static libraries are more simple leaving less possible issues to run into. Before choosing a library, ask your self a few questions like “Is this a large file?”, “Will I need to make updates to it?” and “Will I be sharing this library with others?” if yes to those questions, you’ll prob be better off using a dynamic based library. I hope this article clarified a few things with libraries for you, as it can be rather confusing at first sight.

Cheers!

-CW

--

--