How do Programming Libraries Work?

Holden Grissett
3 min readJan 16, 2017

--

When writing code, we often use libraries to help us fill in the gaps when we don’t feel like reinventing the wheel… Which is great! Let’s not reinvent things just for the sake of working hard. That’s DRY on a global scale! So we know why libraries are good, but do we know how they do their goodness?

No? Let me help you!

(This is for Linux systems, things may differ across systems)

The First thing: Static & Dynamic Libraries

Just in case you need a refresher: Libraries are just files with a bunch of functions in them.

So the first thing you should know is the difference between a static library and a dynamic (shared) library. They’re both useful for different things.

Static libraries are better for quickly getting a library up and running, but it also has drawbacks. It gets compiled into every program that uses it. This means you have to manually update every program that uses it when you edit your library. Not fun!

Dynamic libraries are nice because they load only 1 copy of themselves in memory when you run a program, so you don’t eat up as much memory when you start running multiple programs using that library. The other benefit is that your programs don’t need to be recompiled when you recompile your shared library.

Still interested? Nice! Now let’s make one.

Making a Dynamic Library

In this article I’ll go over dynamic libraries. I made a nice article on static libraries that explains how to make and use them.

First things first: we need to get all of our functions we want to use in one place. However you do this, just make sure to compile those functions as object files, and use the flag -fpic or -fPIC to let the compiler know you’ll be turning it into a dynamic library.

It should look roughly like this:

gcc -c -fPIC -Werror *.c

  • -Werror just throws you an error in case there’s something you should fix in your functions.
  • *.c should be whatever .c files you want included in your library.
  • -c turns all the files into object files. They have the same name but end in .o instead of .c

If everything compiles and the object files look good, then we can turn them into a library!

The command for this should look roughly like so:

gcc -shared -o libmyfunctions.so *.o

  • -shared tells the compiler to make a shared (dynamic) library
  • o tells it to make the output file libmyfunctions.so.
  • Whatever your name is, make sure to prepend it with lib, which will let the compiler know it’s looking at a library file. Append it with .so, which is the shared library output file type.
  • *.o should be whatever output files you made from the previous command

Congratulations, you have a new library!

Using your new dynamic library

So now we have a fresh library called libmyfunctions.so in our directory. We have 2 more things to do before using it.

First: Move the library to /usr/local/lib (if you don’t have that directory, move it to /usr/lib instead):

mv libmyfunctions.so /usr/local/lib

Second: Run ldconfig on the directory you moved it to. This will add our library to a cache which is searched through when a program looks for a shared library:

ldconfig /usr/local/lib

Now we’re set up! Your dynamic library is ready for use.

If you’d like to dig deeper, I suggest reading through this thorough doc from The Linux Documentation Project.

I hope this helped, dynamic libraries are super convenient for functions you need to continually reference for multiple projects.

--

--

Holden Grissett

Software student @holbertonschool, Co-founder @hango, simultaneously Striving & Thriving