Static Libraries and Dynamic Libraries

Victor Rivera
4 min readMay 4, 2020

In this article, I will going into depth about both Static Libraries and Dynamic Libraries in C Programming. A note: All of this will be done on Linux/Bash command prompt as this is where you would create Static and Dynamic Libraries rather quickly and efficiently.

What is a Static Library?

The following is a paragraph from a previous article I published before that was solely on the topic of static libraries:

Firstly, what is a Static Library? Well it’s quite literally a file that collects something called Object Files. Object Files are just machine code complied from the program file put through the compiler; gcc. Now this is the bigger picture: Static Libraries can be used as a single structure when linking a program. It’s a convenient way to turn your c program files executable all in one go, rather than having to go one by one and compile each and every one of them.

There is much more information to cover on Static Libraries which you can read more here:

https://medium.com/@VictorXRivera/about-c-static-libraries-8b3d70a4902a?source=friends_link&sk=7cf00ff2176c84748cb3397f43c8dfb9

Welcome back! Hopefully it was clear enough to understand. Now next I will dive into Dynamic Libraries and break it down step by step.

What is a Dynamic Library?

A Dynamic Library, or for a non-technical term, Shared Library is a collection of routines that are loaded into the application you made at run time. Now upon compilation, the library stays as a separate unit which end with a .so extension. .so means Shared Object Extension.

How to make a Dynamic Library?

So the first step is to create a directory where you want said Dynamic Library by using the mkdir command on the command line. When done, the prompt will look something like this:

So the next step is to bring all of the C Functions you want the Dynamic Library to run into the current directory. Once you’ve either created them or copied them from one directory to the current one, the next step is compile everything. The first command to run is this:

gcc stands for GNU Compiler Collection and is the first command to write on the command line. Next is the -fPIC flag. What this flag does is generate Position-Independent Code (Which is what PIC stands for) that is appropriate for the Shared/Dynamic Library. There’s technically two different ways to write that flag: -fpic and -fPIC. However, -fpic is system dependent so using -fPIC is the more universal option. -c is a flag specifically tells the compiler to compile the C files but only create it’s Object Files (files that end with a .o extension containing non-executable machine code.) Finally, *.c is a special command telling the command line to search for and compile all files in the current directory that end with a .c extension.

The next part is to create the actual library and store all of the object files inside during compilation, like this:

gcc is used again but with different parameters. -shared is used in the process when creating Dynamic Libraries. -o is a flag that tells the compiler to put the output in a file. By default, -o puts the executable file in a file called a.out. However in this regard, we are putting it in our Dynamic Library which is what liball.so means on this command. *.o tells the command line to search for and execute the commands on all files ending with an .o extension.

How to use the Dynamic Library?

All this line of code does is run our main program through the different gcc flags that are checking for any errors like spelling errors, undeclared variables, etc and creating an executable file called len which will run the 0-main.c program using our Dynamic Library. -l is a flag that searches for libraries in the system, specified by the name of the Dynamic Library without the “lib” part.

Differences, Advantages and Disadvantages:

A Static Library gives the user the flexibility to link to programs without having to recompile them, which eliminates the redundant process of having to recompile C Functions or Programs once they’ve made a change to them. Furthermore, it gives developers a sort of security where they can allow programmers to link to the library they’ve made, without having to provide the library source code. Lastly, the code within the Static Library that is linked into an executable should run faster than a dynamic library by 1 to 5% according to sources.

Other the other hand with Dynamic Libraries, many of the programs can share one copy and that saves space on the computer. Furthermore, they can be upgraded to a newer version without having to replace all of the executable files in the library. It uses something called a Import Library which I quote from https://www.learncpp.com/cpp-tutorial/a1-static-and-dynamic-libraries/

An import library is a library that automates the process of loading and using a dynamic library.

Which an share object file (.so) on Linux is both an Dynamic and Import Library.

In a nutshell, Static Libraries are more for speed and eliminating a redundant step, as well as a provide a measure for Developers.

Whereas Dynamic Libraries are more for utility, saving up precious space on the computer and ultimately being very flexible in upgrading and creating it.

I hope this article helps educate on the discussion and comparison between the two. Have a good day!

--

--

Victor Rivera
0 Followers

Hey there, I'm Victor. I'm a student at Holberton School for Software Engineering!