Everything you need to know about Libraries in C

megha mohan
6 min readMay 15, 2017

--

In my last blog post I have discussed about the usage of Static Libraries in C ,when and how to use it. If you haven’t already read about Static Libraries, check it out: All about Static Libraries in C . I have briefly mentioned towards the end about the advantages of Shared Library/Dynamic Library over Static Library. As it was a topic that requires a separate blogpost for itself, here we are, so let us dive a little deeper about Dynamic Libraries and about its advantages over Static Library here.

So to recap, what is a Library and why and how is it used? And what is Static and Dynamic Library?

A library is a collection of items that you can call from your program. You can save much time by reusing work someone else has already done and be more confident that it has fewer bugs (since probably many other people use the libraries too, and you benefit from having them finding and fixing bugs). A library is exactly like an executable, except instead of running directly, the library functions are invoked with parameters from your executable.

You would be familiar about the compilation of a C file. If not check out my blogpost here. The role of Libraries comes in the final step of compilation which is called ‘Linking’. One of the main tasks for linker is to make code of library functions (like printf, strlen, etc.) available to your program. Let us understand the difference between both Static and Dynamic Library and how it is being linked.

Reviewing the Linking of Static Library

This is the most straight forward way of using a library function linking the object files from the library directly into your final executable. Hence the name ‘Static’, because the library will remain unchanged unless the program is recompiled. The final product is a simple executable with no external dependencies.

As a quick refresher, let me list the steps of creating a Static Library below. But if you would like to know in full detail about how to create a Static Library, refer to my blogpost .

Step 1: Compile your Library code(.c files) into an object code

$ gcc -c *.c

Step 2: Create a Static Library from all “.o” files using command ar

$ ar -rc libholberton.a *.o

This tells ar to create an archive (option c) and to insert the objects, replacing older files where needed (option r)

Step 3: Next step is to create index for the library using ranlib command. ranlib makes a header in the library with the symbols of the object file contents.This helps the compiler to quickly reference symbols.

$ ranlib libholberton.

Step 4: We have now created a static library libholberton.a. Now let us use the static library by invoking it as part of the compilation and linking process when creating a program executable. Incase of gcc we use following flags to create static library

gcc main.c -L. -lholberton -o main
  • L : specifies the path to the library .We can use -L. inorder to point to the current directory and -L/home/tmp to point to the /home/tmp directory.

Now run the executable program ‘main’

$./main

So here are the advantages of using Static Libraries.

  • Static libraries are not required at runtime, so you do not need to include them when you distribute your executable.
  • At compile time, linking to a static library is generally faster than linking to individual source files.

Static linking is very straight forward, but has a number of drawbacks.The two main disadvantages are..

  • If the library code is updated (say, to fix a bug) you have to recompile your program into a new executable.
  • Every program in the system that uses that library contains a copy in its executable. This is very inefficient (and a pain if you find a bug and have to recompile, as per point one).

Reviewing the Linking of Dynamic Library

Simply put, A shared library/ Dynamic Library is a library that is loaded dynamically at runtime for each application that requires it. Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory.

Dynamic libraries have advantages and disadvantages as well.

  • They load only a single copy of the library file in memory when you run a program, so a lot of memory is saved when you start running multiple programs using that library.
  • The other benefit is that if any update is made to the functions inside a Library, your programs don’t need to be recompiled ,whereas incase of Static Library you had to recompile or else the update wont be reflected. This type of runtime dynamic linking allows for easier library maintenance.

A major disadvantage of Dynamic Library is that they take more time to load and get the program running because the linking occurs at runtime instead of during compilation. Since there will be no dynamic querying of symbols in static libraries, many softwares still use Static Library for better speed.

How to create Dynamic Library

Now that we know the difference between a Static and Dynamic Library, let us learn in detail about how to create a Dynamic Library and make the executable run.

The steps of compilation of Dynamic Library is almost similar to that of Static Library but with a fe extra steps.

Step 1: Compile all the C files in your current directory using the below command

gcc -Wall -Werror -c -fPIC *.c -o dynTest.o

As you can see, the command is very similar to that of Static Library, extra option here is -fPIC. This makes position independent code (PIC), which is a requirement for dynamic library. We first compile our c code but do not link them, by giving the -c option. The -Wall and -Werror options are just to enable warnings and convert them into errors.

Step 2: Now we have an object code dynTest.o in our current directory. We need to convert this into a dynamic(shared) object file which can be linked with other objects to form an executable. This is accomplished using the following command.

$ gcc -shared -o dynLib.so dynTest.o

The -sharedoption creates a shared object(.so) which can be linked with other objects to form an executable.The -ooption allows us to choose the output filename(Here dynLib.so). The .so suffix stands for Shared Object, and this is convention for Dynamic Library names.

Step 3: This creates a Dynamic Library file called dynTest.so in your current directory. Next step is for us to understand how to use this Dynamic Library that we have created. Since Dynamic Library is linked during runtime, we obviously need to make this file available during runtime. The dynamic linker searches standard paths available in the LD_LIBRARY_PATH and also searches in system cache (for details explore the command ldconfig) for dynamic libraries. So our next step is to add our working directory to the LD_LIBRARY_PATH environment variable so that the linker can find our library file. The below command is used for this.

$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/my_path/

If you do not want to export your current path to LD_LIBRARY_PATH for the dynamic library to work, there is another way to make it work. Move the library to /usr/local/lib because ‘/usr/local/lib’ is already a path specified in the ‘LD_LIBRARY_PATH’ environment variable. Now we need to run ldconfig on the directory you moved it to. This will add our library to a cache which is searched through when a program looks for a shared library:

$ ldconfig /usr/local/lib

Now we are all set! Our dynamic library is ready for use. We can compile and run our program myfile.c now using our dynamic library dynTest.so.

$ gcc -Wall -Werror -o myfile.c dynTest.so

--

--