All about Dynamic/Shared Libraries in C

Diego Jeanluck Linares Castillo
5 min readMay 17, 2022

Types of Libraries

Exists two types of libraries on C:

  • Static libraries
  • Shared / Dynamic libraries

A library is just a file that contains a collection of ordinary object files (.o files) that are linked into the program during the linking phase of the compilation.

Why use Libraries?

We use libraries to reuse the code and not go to the directories to copy the code or the same file and paste it into our current file or actual directory.

So here come libraries to the rescue, we can use them to access our functionality instead of using a library made by someone else. If we do this we can write less code and commands as well as get more order in general.

In addition, we can create the libraries mainly to be more organized or to work as a team on large projects it is easier to work using libraries.

Some disadvantages and advantages about Dynamic/Shared libraries:

Advantages:

  • The size of executable files is very small compared to executable files created from static linking
  • If someone make a update/change to the library isn’t necessary recompile the program.

Disadvantages:

  • The executable depends of the library, because without the library the program can’t access to the definition of the functions in the run time process.
  • What happens when we have to call a lot of functions of different libraries and we have to give the program to the costumer. Exactly we have to give him the program and all those libraries. So in that case use Shared / Dynamic libraries is a disadvantage

How it’s works?

Dynamic Linking Simplify

Dynamic Linking

In the phase of linking, if we are using a function of a shared library and this function exists in our header file, the compiler will go to search the dynamic symbol of that function(s) in the dynamic library.

The symbols are the metadata about the address of the object files

To finally add the address of the function(s) on memory loaded in memory to our source code and get the output of the compiler. The executable file is in machine code (binary code) with the source code and the address of the function(s).

In the run time process the program have to use the functions of the library, so the program go to search the function(s) in the memory.
If the program doesn’t find the library will crash.

How to create them?

Simply way to create a Dynamic librarie

1. Get The Object code (.o)

First, we have to compile our C programs and get the object code (.o files)

gcc -c -fPIC *.c

This command compiles all .c files of the current directory, stops the compilation before the phase of linking and emit position-independent code, suitable for dynamic linking, in this way we can get the object code.

2. Create the library

gcc -shared -o <name of the library> *.o

Well, step by step we are going to understand the snippet of code:

  • gcc = GNU Compiler Collection
  • -shared = This flag produce a shared object which can be linked with other objects to form an executable
  • -o = This flag change the name of the output of the command (by default a.out) but in this case we’ve to set the name of the library.
  • <name_of_the_library> = This can sound obvious but is important for the libraries to use the prefix “lib” and the extension “.so” to use them, so we have to take into account this.
  • *.o = Make reference to all .o files in the current directory

3. Check the contents of the library (List the symbols)

Here is the last step, after all of this we are going to use the next command on the terminal “nm”. This command list the dynamic symbols from the object files (.o) or in this case of our library.

nm -D <name_of_the_library>

The flag -D means “Display the dynamic symbols instead the normal symbols”

And that’s all! You created your first dynamic library but that’s not all now we’re going to learn how to use our new library so let’s go for it.

How to use them?

Well after creating our library we have to use it and for that, we have to use a C file (.c) that uses 1, 2, or more functionalities created by ourselves.

1. Create our main.h file

The .h file is an header file, this file contents the prototypes of our programs or even libraries like <stdio.h> or <string.h>, etc.
You need to have something like this on your header file.

#include <stdlib.h>
#include <stdio.h>int _putchar(char c);
char *create_array(unsigned int size, char c);
char *_strdup(char *str);
char *str_concat(char *s1, char *s2);
int **alloc_grid(int width, int height);

To use this header file we have to use the next line on our C files in case of our header file was called main.h.

#include "main.h"

After this line, we can use the prototypes (functions) and the above-mentioned libraries.
For example:

#include "main.h"
int main(void)
{
printf("Hello World");
return (0);
}

We use the <stdio.h> library but in our C file (.c) are not included but yes in our header file. But is for that we can use printf because the main.h calls to the <stdio.h> library.

2. Create our test file

In this step, you need to create a C file (.c) that uses the header file and any of the prototypes.

3. Compile using our library!

Finally, we’re here so for compile our file using the library using the next command.

gcc -L. <.c file> -l<name of shared library>

Well, step by step we are going to understand the command:

  • gcc = GNU Compiler Collection this is the compiler we’re going to use.
  • -L. = This flag is to indicate where the library is located, the dot is for saying that the library is in the current directory.
  • -l<name_of_library> = This maybe sounds obvious but this flag indicates that the next command will be a library. You have to write the file the next way (in case of our library’s name is libtest).
gcc main.c -L. -ltest

We have to write the library this way because the “-l” flag is like that we have lib”…”.a and we have to complete the name of the library, in this case, is with test because the library name is “libtest.so”.

Visit my other blog if you want to know more about Static Linking / Static Libraries.

Thank you so much for reading.

--

--

Diego Jeanluck Linares Castillo

Autodidact | Passionate about technology | Holberton School Student