Differences between static and dynamic libraries

First, we need to understand what is static an dynamic library

Static library in C is a container objects like a normal library but, the difference is that in a C static library your objects are the code of all your functions that you create for your code that you want to execute, and example could be and static library for 2 functions hello.c and world.c that you call in a main c file. The library extension is .a because is an archive (for example lib.a)

If you don’t use a library you have to compile the C functions like that:

gcc main.c hello.c world.c

For this reason is better use a library to put many functions in there.

we are going to see how to create a static library and how It works.

First step: Compile the functions that you want to put in your library until assembler step of compilation process (do not link for this you should use -c). This happens because with need to create a library first, because before that we are going to link main file with functions. The sintax is:

gcc functions.c -c

For example

gcc hello.c world.c -c

Later, we have a hello.o and world.o ready to put in the library.

Second step: Create the library with the archiver, in this step we use a command ar, It allows create, extract and modify from archives. and with some flags you can create or replace the files -rc (“r” replace “c” create “rc” replace or create). The sintax for create or replace is:

ar -rc libraryname functionscompileinfirststep

Continue with our example:

ar -rc libproject.a hello.o world.o

or if we can use wildcards to put all functions in current folder you can use *.o

ar -rc libproject.a *.o

Third step (for some compilers): Index to archive, the command to index to archive is ranlib an the sintax is:

ranlib libraryname

With our example:

ranlib libproject.a

However, in many cases yo don’t need to use ranlib because the archiver already did it.

Last step: Compiling everything, compile c main file and the library with this recommended sintax:

gcc namecmainfile -L. -librarynamewithoutib -o exename

  • -L “is for locate the files in the current directory”
  • Its better always has the prefix lib and use the flag -l to search the library with this characters
  • -o for indicate the name of exe.

With our example:

gcc main.c -L. -lproject -o projectx

Dynamic or shared library in C

Collection of objects files that are linked into the program during this two steps:

1- >During compile time the linker (process to linker in compilation) verifies all the symbols (Functions, variables) requiered by the program.

2-> The address of each function go to save in exe file (we are going to explain with more details later)

How it works?

When a program (exe) is started the system call a dynamic loader to search the libraries linked with the program and attaches them to the copy of the program in memory. It search in $LD_LIBRARY_PATH (enviroment variable where should be all libraries)

Steps to create dynamic library:

  1. Compile functions

2. Compile and create library

gcc -shared namelib.so *.o

For example we are going to create a library call LIBH:

We are going to compile without linker all functions (*.c to compile all c files in) in current directory

gcc -c -fpic *.c

Now, we are going to create the library compiling all functions in object code (*.o) with the flag that I had describe before:

gcc -shared *.o

Commands to use with your dynamic libraries:

nm List all objects in al library

nm file...

ldd prints the shared objects address

ldd [option]... file...

ldconfig Create update and remove symbolic list for the current shared libraries in /etc/ld.so.conf

ldconfig    [-DvnNX]    [-f conf]   [-C cache]   [-r root]

if you want to create


Memory use: Static compile all the code for this reason use more memory that dynamic because dynamic only sabe the address of each symbols that the program needs.

Compile: If you want to create your exe file or program you need to compile the library and later you compile the main with the library for the both process.

Recompile: If you change something in your functions in Dynamic you only have to recompile the library while if you have a stativ library you should compile library and the main (new exe).

Exe contain: Dynamic library in exe has the library (only with address to functions) and main in binary code and in the static library the exe has all functions, main and library in binary code.

Fullstack developer and sound engineer, learning ML