Static vs Dynamic libraries in C!

What are they?

Programmers are lazy. They go through great lengths to automate and streamline the tedious everyday tasks that, when repeated over and over again, take up an increasing amount of time and keystrokes. One of the things programmers use the most are functions. C has a handy little trick up it’s sleeve when it comes to calling functions more efficiently, namely using libraries both static and dynamic. They are both ways to organize functions into a tidy, organized bunch that can be easily called upon when needed. The biggest difference boils down to this: static libraries are compiled into a program while dynamic libraries are shared and therefore exist outside, independent of the code being compiled.

Both have their advantages and disadvantages. For starters, the nature of compiling the library into the output file means that any change to a static library involves a second step of re-compiling. Meanwhile, dynamic libraries are easy to modify and pass on to the necessary files. However dynamic libraries require more processing power at compile time, and also carry a higher risk of failure, as one bad file at execution will render the whole library useless. Static libraries come out on top in this regard as they are processed much faster at compilation.

Creating a library:

To create a dynamic library, first you need your functions, preferably in individual .c files. Summon the compiler with:

 gcc *.c -c -fPIC

which calls for every file ending in .c that hasn’t been linked (that’s the ‘-c’ part) regardless of where they are located in memory (and that’s the ‘-fPIC’ part). This will generate one object file (.o) for every .c file compiled. These files need to be compiled as well, so we call gcc *.o -shared -o This commands takes every .o file and creates a dynamic library (-shared) where liball is a generic name for the .so file it outputs.

To create a static library: follow the same steps to create the object files and then simply archive the library using:

ar rcs liball.a *.o 

‘ar’ is the GNU archive program and rcs breaks down to the following: r- rewrite if existant, c- create if non-existant, and s- sort it.

Using a dynamic library:

To use a dynamic library we must call it on compile as follows:

gcc -L. example.c -lholberton -o example

In this example -L tells it to expect library files and -lholberton tells it to expect them to come from a file.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Intro to Python Boolean and Conditional Statements


The Importance of Fast Web Hosting

Back to Basics II: What is a Business Rules Management System?

Business Rules Management System

Exploring Oracle Spatial Studio

Integrating LVM with Hadoop and providing Elasticity to DataNode Storage

Introducing Airtable Blocks

Python-compatible IDE: What is It and Why Do You Need It?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gabriel Vazquez

Gabriel Vazquez

More from Medium

Provision a Object Storage cluster with Leaseweb Dedicated Server API

Generate JWK and PASERK easily by “mkkey”

Kubernetes Architecture in a Nutshell

Understanding Kubernetes Multi-Container Pod Patterns and Init- Containers