Dynamic Libraries

Lisa Olson
3 min readApr 17, 2018

What are libraries?

Libraries are a way of condensing code into smaller units. Instead of having multiple files, we can have one single file that contains multiple object files. The way it works is when a C file is compiled, part of the compilation process is the linking phase. During this phase, functions used within the program are checked in a library to see if the code exists or not. There are two types of libraries used in this way. The first is called a static library. The second is called a dynamic library.


If you’re wondering how these libraries could be useful, an example would be sheet music. Rather than grabbing each page of sheet music right before we need it, we can condense all the pages into one single, ready-to-use binder that we would then grab when needed.


The main difference between these two libraries is that a dynamic library, otherwise known as a shared library is referenced as the program is running. This means there’s a dependency upon the application and the library. In a static library, all of the code within the library is copied over to the application. So in a static library, the library file becomes irrelevant once it has been copied, making the application completely independent.


The good thing about static libraries is that you can hand someone your program and it’s entirely ready to go. There’s nothing the user needs to download or install to make your program work. It’s fully autonomous. The bad thing about static libraries are that it makes your program way bigger, taking up much more space than using a dynamic library would.

A dynamic library is great because you don’t need to recompile or relink the applications using certain functions if they change, as you would need to do in a static library. Shared libraries can also be called by programs written in different programming languages. A disadvantage to a shared library is that your application is dependent on tons of files that aren’t local to your individual program. This means you have to ensure everything’s updated and in sync and that there’s documentation on what was required for installation to run your program.


Dynamic :

So, to create one of these dynamic libraries (Linux), the command is fairly simple:

gcc -g -fPIC *.c -shared -o liball.so

That gives you a dynamic library. Let’s go over the flags.

  • g → includes debugging information
  • fPIC → “Position Independent code”; this allows for the code to be located at any virtual address at runtime
  • shared → creates the shared library with the prefix lib and the suffix .so, standing for “shared object”

Static :

To create a static library, the command is :

ar -rc libraryname.a file.0 file2.o file3.o

cc main.o -L. -libraryname -o program

  • ar → archiver; used to create static libraries
  • -rc → tells ar to replace older object files with new ones and to create the library if it doesn’t already exist
  • cc main.o → creates program using main.o and attaches it to the name of the library
  • L → tells linker it may be found in the current directory

Hope that helps clear up some of the fogginess around libraries’ functionality and the differences between dynamic/static libraries. Happy coding!



Lisa Olson

Front End Developer. Passionate about everything I do. How we spend our days is how we spend our lives.