Static Libraries vs. Dynamic Libraries in C

msalem311
3 min readDec 14, 2020
Static libraries are faster because the the code is included in the executable, whereas multiple applications can use the same dynamic library and do not require their own copies.

Libraries are one of the most useful tools that programmers use on a daily basis. A library is a collection of functions and data structures grouped together so that they can be used and referenced whenever the need arises. For example the header file string.h defines a reusable group of functions in the C standard library used to manipulate strings , such as strcmp() (compares two strings) or strlen() (finds the length of string).

There are two types of libraries in C. There are static libraries, and dynamic or shared libraries. A static library is a library that is copied into another program at compilation time producing one executable file. A dynamic library is a library that is stored in special place in the computer’s memory and can be accessed at the same time by multiple programs.

The advantage of static libraries is that they cannot be changed without recompilation, so the program will always have the library it needs because the library becomes part of the program at compilation. A big disadvantage is that because the static library becomes part of the program at compilation, each program that uses it needs it’s own copy, which can take up an unnecessary amount of memory.

This problem was solved by creating dynamic libraries. As you can see in the photo above, the same dynamic library can be used by multiple programs at the same. This takes up less memory because there is only one library that be used as many times as needed. A disadvantage is that if somehow the library gets changed and something gets deleted, the dependent programs will not work.

Creating Libraries

The first step to creating a library is to gather the source code of all the C files that you want to be in the library into one directory.

Static Library

To create a static library, you need to run the command: gcc *.c -c. This makes the object files for all the C files in the current directory. You then run the command: ar -crs NameOfLibraryHere.a *.o. This takes all of the object files and puts them all in the static library. To use your static library, just include the name of library in the arguments to gcc. Example: gcc YourFileName.c YourLibraryName.a

Dynamic Library

To create a dynamic library, run the command: gcc *.c -c -fpic. This makes all the object files for the C files in the directory. Then run the command: gcc -shared -o YourLibraryName.so *.o. After creating the library, you can link it to the file by using: gcc -L. YourFile.c -lYourLibraryName

Make the Shared Library Available

To make the library available you need to change the LD_LIBRARY_PATH variable. You have to add the current working directory to the start of the variable by using this command: export LD_LIBRARY_PATH = .:$LD_LIBRARY_PATH. Now you can run your program with your newly created shared library.

--

--