Dynamic C Libraries

 



Introduction

Before starting to talk about the subject matter, let us take a brief look at the compilation phases of a C program.
There are basically four phases:

  1. Pre-processing
  2. Compilation
  3. Assembly
  4. Linking

In this article, we will focus on the dynamic libraries and their role in the linking phase of a program.

If you want to learn more about a static libraries and compare them, please visit my previous post 

But, first of all, let us define a library.

What is a library?

A library is a collection of code routines (functions, classes, variables, and so on) that can be called upon when building our program, so instead of writing the same code over and over again, we can archive it and extract it from something that has already been written and optimized.

Why use libraries?

The main reason for writing or using a library is to allow code reusability, thus save considerable development time.
That is where the idea behind libraries comes from. We are reusing blocks of codes that have come from somewhere else.

What is a Dynamic library and how do they work?

A dynamic library is a file containing a collection of object files (*.o for unix like systems) that are referenced into the program during the linking phase of compilation.
The referenced code in dynamic lib is pointed from resulting program to dynamic library code section address and is loaded at runtime (library dependent on run-time).


How to Create a C Dynamic Library on Unix like System

To create a dynamic library, we need to specify to the compiler, that we want to compile all library codes (*.c) into object files (*.o)  without linking.
For this example the compiler is gcc.
gcc -fPIC -c my_math_func.c print_alphabet.c
Flag description:
 
-fPIC: This makes it possible to build shared libraries without position dependent code on architectures this is possible, i.e. on x86.
-c: Compile and assemble, but do not link.

Now we have 2 object files called:
my_math_func.o print_alphabet.o
We can now bundle all object files into one dynamic library.
To create a dynamic library, we have to use GCC program with flag -shared:
gcc -shared my_math_func.o print_alphabet.o -o libname.so

This command creates a dynamic library named "libname.so" and puts copies of the object files "my_math_func.o" and "print_alphabet.o " in it.

Flag description:

-shared: Produce a shared object which can then be linked with other objects to form an executable.

-o:  To define output shared object name.

In order to list the names of the object files in our library, we can use the nd command with -D flag:
nm -D --defined-only libname.so

Flag description:

-D: Display the dynamic symbols rather than the normal symbols. This is only meaningful for dynamic objects, such as certain types of shared libraries.

--Defined-only: Display only defined symbols for each object file.

How to use them?


We can use it in a program. This is done by adding the library's name to the object file(s) given to the linker. First, let us create a C source file that uses the above created static library, for example "main.c":
void print_alphabet(void);

int main(void)
{
        print_alphabet();
        return (0);
}
Now we can use gcc to create our final executable program:
gcc main.c -L. -llibname -o main
Flag description:
-L Specifies the path to the given libraries ('.' referring to the current directory)
-l Specifies the library name without the ".so" suffix, because the linker attaches these parts back to the name of the library to create a name of a file to look for.
 
Finally we have to "install" the dynamic librarie:
export LD_LIBRARY_PATH=<path_to_my_dyn_lib/>:$LD_LIBRARY_PATH
Description:
Using this sentence we are adding the path to environment var LD_LIBRARY_PATH


The output of our compiled program using dynamic library will be:
$./main
abcdefghijklmnopqrstuvwxyz

Comentarios