Image for post
Image for post

Static Library vs Dynamic Library

First we need to know what are libraries? Libraries are simply just files that contain functions, they are not executable. Applications are the ones that can execute, and they are the ones that utilize libraries. Commonly used functions are normally in libraries so that they can be called upon when using the executable application.

Image for post
Image for post

The first kind of library we will talk about is a static library. These libraries are used during the compilation process (one of the differences from a dynamic library). The code from the static libraries are pretty much copied and moved into the application during compilation process (specifically linking).

Pros and cons: This usually means that our executable application will become bigger when utilizing static libraries, but it also means that the application will have all the functions it needs to run by itself. No need to reference to a function, unlike a dynamic library. An application that uses a static library will also need to be updated if the library itself had some changes, since the application has the library of functions compiled into it, this is another of the drawback of using a static library.

Image for post
Image for post

The second kind of library is a dynamic library. These libraries are used outside of compilation, and are called upon while executing the application. As mentioned in the static library, during the linking part of compilation of an application, the code from the library is put into the application. In the case of dynamic library, the address of the library is copied and moved into the application. Any update done to the libraries will reflect on the application, as the application references the library by memory, which makes it more dynamic when updating.

Pros and cons: Application is generally not as big, since you have the address of the library instead of the entire copy of it. However, the application will rely on the library being there when needed, otherwise the application won’t work.

Image for post
Image for post

Now we will get into creating dynamic libraries and using them: the command for this is . The -g flag is for debugging info information, and the -fPIC flag stands for “Position Independent Code”, a requirement for dynamic libraries. Lastly, the -shared flag creates the shared library ( for shared object).

Using the created dynamic library: the command is . This will compile to your named app with your main file and your library name file.

Written by

Software Engineer | Linux | Cats | https://github.com/kai-dg | https://haruspace.dev

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