Static and Dynamic library in C

Valentina Carrillo
4 min readJul 15, 2019

Why use libraries?

libraries allow us to store function definitions, aka pieces of code that will be used in our executable program. In programming, it is very common to reuse function codes and for that we have libraries.

Some examples of popular libraries are:

  • stdlib contains a lot of common functions such as atoi.
  • math a library of functions that perform math calculations.
  • stdio contains functions that read or write to a file and more.
  • time the library contains functions to work with time.

How do they work?

Once the libraries are created, they can be linked with the file containing the main function, or entry point, with gcc. Then, the code of the functions used in the program will be linked into the executable program, and it will be ready to run.

Static libraries are a collection of object files that are linked during the linking phase of the compilation and create an archive file (.a) Ultimately making it easier and efficient for the compiler and user to call there functions when needed.

Dynamic Libraries are similar to Static libraries in the sense they both are built form several object files (.o). However, unlike the archive the object files are properly linked together (referenced by memory address of the functions) in a dynamic library to form one single piece of object code.

Creating a Static Library

Compile all your C files into object files.

gcc -Wall -pedantic -Werror -Wextra -c *.c
  • -c : Compile or assemble the source files, but do not link.
  • -Wall : Include all warnings.
  • -Wextra : Enables extra warning flags that are not enabled by -Wall.
  • -Werror: Make all warnings into errors.
  • -pedantic : Issue all the warnings demanded by strict ISO C and ISO

Now use the ar program in order to create an archive.

ar rcs lib_mylib.a *.o
  • r: To insert the files into the archive.
  • c: In order to create the archive.
  • s: To write the object files index into the archive or update an existing one.

Optional:

Use ranlib in order to generate index to the archive. Note: Notice how ar s does the same thing as ranlib? That is because running ar s on an archive is equivalent to running ranlib.

ranlib lib_mylib.a

Creating a Dynamic Library

Compile all your C files into object files

gcc -fPIC -Wall -pedantic -Werror -Wextra -c *.c
  • fPIC: This is a requirement for shared libraries that stands for “Position Independent Code”.
  • -c: compiles source files without linking.
  • *.c: selects all of your c files.
gcc -shared -o lib_mylib.so *.o

The -shared flag tells the compiler to produce a shared object which can then be linked with other objects to form an executable and the -o flag simply takes the following argument and uses it as a filename for our output. we now have a dynamic library!

Once you create a shared library you will have to install it.

Usage:

nm -D lib_mylib.so
  • Use this command to see the functions in your libraries.
  • -D: This refers to the symbols in the data initialization section
  • Update your LD_LIBRARY_PATH to include the directory where your library can be found
gcc -L. main.c -l_mylib -o run
  • Compile your program and include -L. to include the path to your library
  • Exclude the lib prefix and .so suffix to include your library

The command above will make an executable file for main.c and use any functions in our lib_mylib.alibrary. The -L. flag tells the linker that the library we are using is in our current directory. The -l_mylib a portion of our command is actually our library’s name sans the liband .a with a -l stuck to the front of it. The linker adds those portions of the library name back while searching for the library file.

Before running your executable, it is also important that you create an environment variable to your library path so it can be found. You can do so using the following command:

$LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

Now your program is linked to your dynamic library!

To Summarize:

Static Libraries

  • Get copied directly into your executable.
  • The executable is not updated automatically when there is a change in the library.
  • Larger executable files.
  • Uses the ar and runlib commands.
  • Have a prefix lib and an extension .a.

Dynamic Libraries

  • Only the address of the library is referenced in the executable.
  • Executable gets updated when a new version of the library is encountered.
  • If versioning is not used, It will overwrite the previous library versions, possibly breaking your executable.
  • Smaller executable files.
  • Uses the gcc with the flags -fPIC and -shared.
  • Have a prefix lib and extension .so.

--

--