What are Libarys in C and why use them ? (Dynamic vs Static libs)

Manuel Enrique Figueroa
5 min readDec 14, 2020

--

What are Libarys in C & Why should we use them ?

In order to understand C libarys we have to go all the way back to see how a C program is compiled. As you can see in the attached image below is the C compilation proccess. We are gonna focus on what is known as the Linker stage of the proccess. Here all of our functions from our header file and our current main file are linked together essential after each has gone through its own preprocessing, compilations and assembley sections. Then all object files are linked together in the final exe. What a C libarys does is allows us to create a collection of these object files and use them accross different programs with the need to create new/multiple object files for multiple compilations. It is a more intuitve way of using custom functions accross files by having them all in one place and keeps us from repeating unnecessary tasks within the compilation proccess.

The C compilation proccess

Whats the difference between a Static lib and a Dynamic lib ?

In order to understand the difference we must talk a little bit more in depth into what an actual static lib does. A static lib essentially takes our object files from our library and copys them individualy into our executable during the linking proccess in turn this can take up more memory and increase compile time overall. However it is more “dynamic” in different environments since you do not need to worry about the library changing or if the libary is present on the users system for the executable to run.

A dynamic library does not create these individual copys for each exectuable the libary and main file are linked at the point of execution of the exe. This in turn requires both the compiled main file and the “linked” library to complete the execution proccess.

This is a diagram the kinda explains how a libray is used by a executable.

How to make a Dynamic Library:

Making a dynamic libary uses the command (( gcc [name_of_c_file.c] -c -fPIC )) which makes the given file or like the command in the photo above all the C files in the directory into object files using the -c gcc option. Then using the command below we can actually make the libary with all the object files in the directory. usage (( gcc [object_file.o] -shared -o [library_name.so] )) this uses the -shared option for gcc which creates the library and the -o option to name the output in this case liball.so . All dynamic libs have the extension .so at the end so dont forget to add that when creating one.

However we are not finished yet in order to actually use the dynamic library on our users system we need to add the directory where the library is located in the environmental variable on linux. This can be done by using the command below. Usage {{expot LD_LIBRARY_PATH=.}} the dot in the usage is just the working directory you are calling the command from which will be most likley where your library already is but you can do this multiple ways.

First way using the dot (current directory) if the library is in the current workind directory
The second way using current directory of the libarys
The third way using the working directory of wherever the libray is located aka the path of it.

By using these commands above we are able to tell the linux system where we can find the shared libray so when a executable is called it can be linked.

How to make a Static Library:

Creating a static libray is a little simpler than creating a dynmaic library since we dont need to linke the library to the executable. Since we dont need to do this we are not required to add the libs path to the LD_LIBRARY_PATH variable in linux. We begin creating a static library by make all of our C function files into object files just like a dynamic lib by using this command

To make all c files object files

Then all we need to do is call this command ar rc which is outlined below with all of out object files that we created usage {{ar rc [name_of_library.a] [object_files.o]. and just as before all static libs have the extension .a at the end of them so dont forget to add it at the end of the name.

creates a static lib called liball.a using all the object files in the current working directory

Headers:

when you create the librarys file headers that are usually end with the extension .h containt all of you function prototypes are required at the top of every main file that uses the library in order for it to be linked.

Congrats you now know everything you need to know about Dynamic and Static Libs enjoy !!

--

--

Manuel Enrique Figueroa
Manuel Enrique Figueroa

Written by Manuel Enrique Figueroa

0 Followers

I am a current student at Holberton school of software engineering aspiring to have a career in full stack software engineering.

No responses yet