Photo by Kevin Ku on Unsplash

Static and Dynamic Libraries (differences and more…)

In computer science, a library is a collection of non-volatile resources used by computer programs, often for software development. These may include configuration data, documentation, help data, message templates, pre-written code and subroutines, classes, values or type specifications.

A library is also a collection of implementations of behavior, written in terms of a language, that has a well-defined interface by which the behavior is invoked. For instance, people who want to write a higher level program can use a library to make system calls instead of implementing those system calls over and over again. In addition, the behavior is provided for reuse by multiple independent programs. A program invokes the library-provided behavior via a mechanism of the language. For example, in a simple imperative language such as C, the behavior in a library is invoked by using C’s normal function-call. What distinguishes the call as being to a library function, versus being to another function in the same program, is the way that the code is organized in the system.

Differences Between Static And Dynamic Libraries

How Works a Static Library

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 libraries are either merged with other static libraries and object files during building/linking to form a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset determined at compile-time/link-time.

How Works a Dynamic Library

A dynamic library (shared library or shared object) is a file that is intended to be shared by executable files and further shared object files. Modules used by a program are loaded from individual shared objects into memory at load time or runtime, rather than being copied by a linker when it creates a single monolithic executable file for the program.

Shared libraries can be statically linked, meaning that references to the library modules are resolved and the modules are allocated memory when the executable file is created. But often linking of shared libraries is postponed until they are loaded.

How to Create Dynamic Libraries in Linux

  1. Create the object code running the command

The above command gets all the .c source files located in the current work directory and return objects file .o

2. Create library executing the command

In this step is used the flag -shared for all the object files .o in the current work directory which compile them into a dynamic library. The compiler identifies a library by looking for files beginning with ‘lib’ and ending with a library extension (.so for dynamic libraries, in this case our file is named

3. Set the value for $LD_LIBRARY_PATH

In this case we let to know to the program where to look for library files adding the location to the environment variable $LD_LIBRARY_PATH.

How to Create Static Libraries in Linux

  1. Code your functions in separate C files “file.c”
  2. Compile the C files as object files “file.o” or “file.obj”. We can use GCC with the parameter -c

The -c option stop the compiler and producing the .o files, the compiler will create the objetc file “file.o” based on the C file “file.c”

3. Create the library (for our example we will name the library as libra.a)and add the object files, using this command

The ar command create the archive file (libra.a), -r is the parametrer used to replace-overwrite .o files (in case library exists already), -c creates the library if it doesn’t exist or appends to it if it does.

In this moment we have the library libra.a created.

Additional useful commands

- After creating the library it was once necessary to run the command: ranlib ra.a. This created a symbol table within the archive. Ranlib is now embedded into the “ar” command.

- The command “nm” lists symbols contained in object files

- The ldd command prints shared library dependencies.

- The ldconfig command configure dynamic linker run-time bindings
ldconfig -p : Print the lists of directories and candidate libraries stored in the current cache.
i.e. /sbin/ldconfig -p |grep libGL

- gcc also has several options that could be useful when we work with libraries for example with options -l ( -llibrary or -l library) search the library named library when linking, and -L (-Ldir)add directory dir to the list of directories to be searched for -l (For example gcc -Wall -L/opt/lib file.c -ltest -o file). You can additional information checking the gcc man in the section Options for Linking.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store