Static Libraries vs. Dynamic Libraries

jassem ben ali
3 min readSep 20, 2021

In programming, a library is a collection of pre-compiled pieces of code that can be reused in a program. Libraries simplify life for programmers, in that they provide reusable functions, routines, classes, data structures and so on
which they can be reused in the programs.

Static Libraries :

A Static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. This executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static.

Static Library Creation (Linux only)

Creating a static library is much simpler. First, create the object files the same way as step 1 above. Then archive the library using ‘ar rcs liball.a *.0’

Your program should include a prototype for each of the functions that exist in your library. If you’re using a header file for these prototypes, make sure to include the name of that file in your other files by using #include “<header file>”

Program compilation
gcc -L. -lall -o my_program main.c

When compiling program files, we have to tell the compiler to use the library files and where to find them. ‘-l’ tells it we want to include library files. And ‘all’ tells it to look for the library liball.so. It’s important to leave the ‘lib’ and ‘.so’ out of the flag because the compiler already identifies library files that way. ‘-L.’ tells the compiler it can find the library file in the current directory.

Program compilation
gcc -L. -lall -o my_program main.c

When compiling program files, we have to tell the compiler to use the library files and where to find them. ‘-l’ tells it we want to include library files. And ‘all’ tells it to look for the library liball.so. It’s important to leave the ‘lib’ and ‘.so’ out of the flag because the compiler already identifies library files that way. ‘-L.’ tells the compiler it can find the library file in the current directory.

Dynamic Library Creation (Linux only)

  1. gcc *.c -c -fpic
    The .c source files need to be prepared for use in a dynamic library. Since multiple programs can all use one instance of a dynamic library, the library can’t store data at fixed addresses. This is because the location of the library in memory will vary between programs. This is done by using the compiler flag -fpic. Since we need to apply this step after the compile process has generated the object code, the compiler must be told to halt and return one object file (.o) for each source file. This is done by using the -c flag.
  2. gcc *0 -shared -o liball.so
    The object files are now ready to be compiled into a dynamic library. This is done by compiling all of the .o files using by using the -shared flag. Later when compiling program files, the compiler identifies a library by looking for files beginning with ‘lib’ and ending with a library extension (.so for dynamic, .a for static). Therefore it’s important to name a library accordingly.
  3. export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
    Because a program needs to know where to look for library files, we must add that location to the environmental variable LD_LIBRARY_PATH.

--

--