Time for dynamic ones

Differences and Advantages

The way dynamic libraries work is that they are 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, but resulting in larger binary files, and needing more space on disk and main memory.

Creation

Let us now take a look at how to create a dynamic library;

gcc -g -fPIC -shared *.c
  • -g → used to 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”

Usage

To use a dynamic library, a path must be added to the library to the environment variable LD_LIBRARY_PATH. To do this, we use the command:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
gcc exemple_C_files.c -L. -ldynamic

--

--

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