Let's C Dynamic Libraries
Differences between dynamic and static libraries
Mon Sep 16 2019 - 4 min read
The first thing to do is to define what is a library, in simple words a library is a set of code that was previously written, that can be called from a program.
Why use libraries
In general, It is work that was previously done by someone else that you can now use it, these libraries serve a specific purpose like math functions or handling strings, in this way you don’t need to re-write the entire functionality, you just have to use them for your specific case and save time.
How do they work
There are static libraries and dynamic libraries (shared libraries), in a previous blog I wrote about static libraries so you can go and read about them in this link Static Libraries. There are some steps of compilation that convert the code into machine language, all the processes start with the preprocessing, compilation, assembly and finally linking, this is where libraries are bound.
In general, libraries are created from many source files and are either built as archive files (files .a in case of Linux), that are statically linked into executables that use them or as shared object files (files .so in case of Linux) that are dynamically linked into executables.
How to create them
First I am going to review how to create a static library in C.
Using $ gcc -c namefiles.c
would create the object files .o from the .c files that you want to save as a library. Now that you have the object files you can use a program called ar, for archiver, you can use it like this
$ ar -rc libpersonal.a *.o
This command creates a static library named libpersonal.a and puts copies of the object files on the current directory. The r flag tells ar to replace older object files in the library with the new object files and the c flag tells to create the library if it doesn’t already exist. You should use ranlib to re-generate the indexes.
Now let’s review how to create a dynamic library in C.
The object files for the dynamic library needs to be compiled as position independent code (-fPIC) because they are mapped to any position in the address space $ gcc -c -fPIC *.c
.
To create the dynamic library you can use gcc -shared flag and name the result file with .so as follows
$ gcc -shared *.o -o liball.so
How to use them
After the static library is created, it has to be added to the list of object file names given to the linker, using the -l flag.
$ gcc main.c -L -lpersonal -o alpha
Note that it is omitted the lib prefix and the .a suffix when mentioning the library on the link command. The linker attaches these parts back to the name of the library to create the name of a file to look for. The -L flag tells the linker that libraries might be found in the given directory, in this case, is the working directory.
As long as the shared library is not installed in a default location like /usr/lib, you should indicate where it is found, this is possible with the LD_LIBRARY_PATH environment variable.
$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
And finally
$ gcc -L. main.c -lall -o len
This command compiles the main.c and links the code with liball.so.
Static libraries vs Dynamic libraries
Static libraries are bigger in size than dynamic libraries because external programs are built in the executable file, dynamic libraries are smaller because there is only one copy of the dynamic library that is kept in memory.
When using static libraries the executable file will have to be re-compiled if any change is applied to external files, meanwhile the dynamic libraries there is no need to re-compile the executable if a change is made.
The static libraries take longer to execute because loading into the memory happens every time while executing meanwhile dynamic libraries are faster.
As you can see there are some differences between static and dynamic libraries, there are some advantages and drawbacks using one or the other, dynamic libraries look like the better choice but static libraries make software distribution simpler, I let you decide which one to use in specific cases, and I hope this blog helps understand more about libraries,that’s all for now.