How to reuse code in C programming: Static and dynamic libraries

Why libraries?

When we code in C we create our own functions based mostly on predetermined functions that are included before the main with lines like these:

With time, we are going to identify some pieces of code that are repeated many times, and we can split that part and save it in a new different function, to reuse the same code multiple times. So, we are going to add a list of the prototypes (declarations) of the functions, too.

And later we are going to include macros.

Are you telling me that I have to include all these things in every single function?

No. You only have to save all that in a header, that will become a static or dynamic library to reuse the code, to have more organized projects, and to include all the needed things just once and for all.

Pre skeleton: The header file

To create a library we need to create a header file first with the extension “.h”. It can be created by naming a new file with the extension “.h”, and using this format:

Then it needs to be called before the main function in just one sentence:

Why create libraries?

The bigger your program more files you are going to need to compile.

When you create a static library you encapsulate all the functions, turn them into object code and index them. It is like having a bag to store and move all that quantity of objects, organized and ready for use. You only have to take one bag instead of pick 10 or 20 files until you go out from home. There you have your, phone, your keys, your books, your pencils, and no worries.

Static library

A static library is an indexed collection of functions in the format of object code.

It is a bag that you put inside your program and that bag contains all the other functions your code is based on. Like a pouch, it is full of the most commonly used functions that your code needs.

Ready to be used at the linker phase of the compilation time. Advantages:

  • Faster compilation time
  • Fewer files to look for

First, you turn all the source code in c language, to object code, then, you create a file (a bag) to save them, and then you organize the collection (index). Now your bag is ready to be compiled with the other files of your program.

Dynamic Library

There is a special case. Imagine, that you have that bag but sometimes you need to go to the beach, other times, you need to go to the mountains, and other times you go only to the school. You probably don't want to have three bags with the same content to append to the different other needed stuff.

You want to have that bag separately from the other bags, so you can use it for all the previous purposes. Only one.

That happens to a computer too. It can have many different programs based on the same package of functions, and instead of include static libraries in each, it prefers not to include the same code three times, and instead to have an external library that can be used at the execution time of each program.

The dynamic library can be called from anywhere, it's not linked to the executable file and won’t need 3 times space for the same information. Other advantages are:

  • Can be updated without compiling the program again.
  • One library can be used and shared by many different processes and programs, using less memory.
  • A function can be used without putting the entire library in the memory.

Which code do I need?

Here you go, winner!

Software Developer