← Back to list

The Difference Between Static and Dynamic Libraries

Functions are essential when writing code. They are blocks of code that can perform or automate a task that you regularly need. If you have…

Lindsey Lancaster · 2023-12-21 05:33 · 0 claps · 3.0 min read
#c-programming-language #c-programming #static-libraries-in-c #dynamic-libraries-in-c
Open on Medium ↗
Wiki topics: 💻 · Programming

The Difference Between Static and Dynamic Libraries

Photo by AltumCode on Unsplash

Photo by AltumCode on Unsplash

Functions are essential when writing code. They are blocks of code that can perform or automate a task that you regularly need. If you have a function that you are often calling in your programs, then you can create a library to utilize it in a more efficient way. This can be very handy because it allows you to quickly reference your functions through the library. You are able to implement the functions into your program without having to copy the function’s code directly into your C file. In instances where your program contains many lines of code, this can help make your code more easily readable.

To begin making a library, you will need to create a header file. Be sure to guard your header file. You will declare the function prototype by including the prototype name into your header file. The function prototype is the beginning line of your code. For example, if your code started with “int main(void)”, then you would declare your prototype as “int main (void);”.

An important distinction between libraries and header files is that header files are not libraries, although libraries contain header files. The C Standard Library contains a collection of functions that are declared in multiple header files. For example, stdio.h is a header file in the C Standard Library but it is not a library itself. Again when you declare a function, you are putting the function prototype into a header file. However, the code for your function is contained in a C file that you convert into an object file for your library.

There are two types of libraries known as static and dynamic libraries. It is common to see dynamic libraries referred to as “shared” libraries. Static libraries end in a “.a” extension while dynamic, or shared libraries, end in a “.so” extension. Static libraries are compiled only once and then linked to when the library is being used. In contrast, dynamic libraries exist as multiple files that are recompiled each time they are ran. It is also important to note that static libraries take up more space compared to a dynamic library.

Static libraries are created from C files that contain functions. After these files are created, you put the prototype name into a header “.h” file. Next, you compile the library files and create your object files using “gcc -c examplefile.c”. You could also write this as “gcc -c .c ”, to quickly convert all “.c” files into “.o” files. To create the static library you can use “ar -rc libexample.a .o”. You can run “man gcc” to see all possible flags. Lastly, to index the library, run “ranlib libexample.a”.

After creating a file, you can use the library by running “gcc example_program.c -L. -lexample -o example_program”. The flag “-L.” tells the compiler to look for library files in the current directory. The “-l” flag tells the compiler to link example_program with the library.

Dynamic libraries are also created from C files that contain functions. There is an additional flag “-fPIC” used when creating object files for dynamic libraries. You can run “gcc -fPIC -c .c” to create the object files. Next, you will create the library by running “gcc -shared -o libexample.so .o”. The flag “-shared” tells the compiler that the files will be put in a dynamic library.

The library needs to be shared dynamically, and there are a few options for how to do this. One way would be to add a path to the environmental variable LD_LIBRARY_PATH. You can do this by running “export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH”. The “.” adds the current working directory to the path. Another option would be to use “ldconfig -n -L”.

Now to use the dynamic library, you can run “gcc -L. example_program.c -lexample -o example_program”. Similar to the static library, we use the “-L.” and the “-l” when compiling. However, in this case the “-l” tells the compiler to look for a dynamic library.

After creating your library of choice, you can use the following commands to get additional information. The brief descriptions provided are from the Linux man page of each command.

nm — lists the symbols from object files. If you use “nm” for a dynamic library, you can use the flag “-D” to display only dynamic symbols.

ldd — prints shared object dependencies.

This post has been slightly modified and is a re-post from my previous account. It was originally posted Oct 18, 2022.


메타데이터
post_id
2ecf46f8f4ef
slug
the-difference-between-static-and-dynamic-libraries-2ecf46f8f4ef
url
https://medium.com/@lindseylancaster/the-difference-between-static-and-dynamic-libraries-2ecf46f8f4ef
canonical_url
https://medium.com/@lindseylancaster/the-difference-between-static-and-dynamic-libraries-2ecf46f8f4ef
author_url
https://medium.com/@lindseylancaster
status
ok
fetched_at
2026-08-23 13:57:04