Skander Amireche
4 min readMay 4, 2020

--

The differences between static and dynamic 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.

A dynamic library is a programming concept in which shared libraries with special functionalities are launched only during program execution, which minimizes overall program size and facilitates improved application performance for reduced memory consumption.

Static Library file (Linux only)

Create a static library using GCC to compile the library code into a target file Static:

$ gcc -c *.c

all files that had an extension .o including the initial creation of the library

$ ar rcs libholberton.a *.o

r Replace the files if they already existed.

c Create the files if it does not exist.

s Build an index of content.

check all your obtained files with ar by using this command:

$ ar -t libholberton.a

A static library is purely a collection of .o files put together in an archive that's something like a zip file (with no compression). When you use it for linking, the linker will search the library for .o files that provide any of the missing symbols in the main program, and pull in those .o files for linking, as if they had been included on the command line like .o files in your main program. This process is applied recursively, so if any of the .o files pulled in from the library have unresolved symbols, the library is searched again for other .o files that provide the definitions.

Dynamic library (Linux only)

$ ls *.c

Create Shared Libraries (Linux only)

$ gcc -g -fPIC -Wall -Werror -Wextra -pedantic *.c -shared -o libholberton.so

$ ls

The -Wall -Wextra -pendantic flags are all warning and error flags that should be included when compiling your code.

The -g flag debugging information

man gcc says: (-fPIC)

-fpic
Generate position-independent code (PIC) suitable for use in a shared
library, if supported for the target machine. Such code accesses all
constant addresses through a global offset table (GOT). The dynamic
loader resolves the GOT entries when the program starts (the dynamic
loader is not part of GCC; it is part of the operating system). If
the GOT size for the linked executable exceeds a machine-specific
maximum size, you get an error message from the linker indicating
that -fpic does not work; in that case, recompile with -fPIC instead.
(These maximums are 8k on the SPARC and 32k on the m68k and RS/6000.
The 386 has no such limit.)

Position-independent code requires special support, and therefore
works only on certain machines. For the 386, GCC supports PIC for
System V but not for the Sun 386i. Code generated for the
IBM RS/6000 is always position-independent.

-fPIC
If supported for the target machine, emit position-independent code,
suitable for dynamic linking and avoiding any limit on the size of
the global offset table. This option makes a difference on the m68k
and the SPARC.

Position-independent code requires special support, and therefore
works only on certain machines.

The -o flag

-o filePlace output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler
file or preprocessed C code.

Using Shared Libraries

gcc -Wall -Werror -Wextra -pedantic -L. 0-main.c -lholberton -o len

The 0-main.c to use reference shared library to execute our program

After running the command we got new executable files called: len

there’s one step more you need to do before executing the len file

$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

Dynamic library evolved from the following concept: If multiple applications use certain library functionalities via several lines of code, it is easier to maintain and upgrade different library versions, rather than apply corresponding application changes. Also, because a dynamic library contains several lines of code, establishing a link at compile time helps reduce overall memory and enhance application performance.

Good luck !!

--

--