Dynamic or Shared Libraries

Minas Anton
3 min readAug 9, 2017

As explained previously In computer science a Library is a set of routines, external functions and variables that we store in an indexed archive (file). You may have noticed that we are using functions which are not defined in our code, or in that particular file and we include a header file in the top, that contains declarations of those functions. For example everyone that have wrote a code that prints a message on screen , used printf(); function and included stdio.h to make it work.

There are Dynamic and Static libraries.

Well a Static Library is this archived collection of functions which are resolved in a caller at compile-time and copied into a target application by the linker producing a stand alone executable. This is process is also known as a static build of program.

Pros

  • Decreased loading time
  • Stand alone applications

Cons

  • Increased size of code
  • Increased memory usage
  • For any change-update got to re-compile the whole application

A Dynamic(Shared) Library is the library that can be linked to any program at run-time. They provide a means to use code that can be loaded anywhere in the memory. Once loaded, the shared library code can be used by any number of programs. So, this way the size of programs and the memory footprint can be kept low as a lot of code is kept common in form of a shared library.

Pros

  • Reduced size of code
  • Reduced memory usage
  • Modulatiry- code can be changed without re-compiling the application

Cons

  • Increased loading time
  • Applications can’t work without the library existence
  • Corrupted library might cause the application to fail

How to create and use a Shared Library

To create a shared library you need to compile your .c files ,that include the functions you want to add in the library , into .o assembly files and then compile them in to a library. This can be done simply with the two following lines:

gcc -c -Wall -Werror -fPIC *.c
gcc -shared -o libshared.so *.o

The first command compiles the code of all .c into position independent code which is required for a shared library.
The second command creates a shared library with name ‘libshared.so’.

Now in order to link the library to our application we do the following:

gcc -L/home/username/test -Wall -Werror -o test main.c -lshared

This command now linked our executable to the library , but we are not finished yet. We still have to copy the library to a standard location of the following:

/lib
/usr/lib
/usr/local/lib

after copying it we have to run: ldconfig in order to create a link to the library and update the /etc/ld.so.cache for immediate use.

Another way around it but temporary is to use the enviromental variable:

export LD_LIBRARY_PATH=/home/username/test:$LD_LIBRARY_PATH

One more way but less flexible is to embed the location of the library to the executable itself ,not to the loader, using -rpath like this:

gcc -L/home/username/test -Wl,-rpath=/home/username/test -Wall -o test main.c -lshared

Note that if the library doesn't exist in the designated path app will fail.

We covered what libraries are, how to create and use a shared library differences, positive and negative of both as well as of the ways to use them.

Cheers!!

--

--