Differences between Static and Dynamic Libraries

Differences between Static and Dynamic Libraries

Functions are blocks of code that are reusable throughout a program. These help in saving time without rewriting it whole again. Similarly, libraries like functions also save time by reusing in multiple programs.

When a C program is compiled, the compiler generates object code. After generating the object code, the compiler also invokes the linker. One of the main tasks for linker is to make code of library functions (eg printf(), scanf(), sqrt(), ..etc) available to your program. A linker can accomplish this task in two ways, by copying the code of library function to your object code, or by making some arrangements so that the complete code of library functions is not copied, but made available at run-time.

No alt text provided for this image

Static Linking and Static Libraries is the result of the linker making a copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory. Examples of static libraries (libraries which are statically linked) are, .a files in Linux and .lib files in Windows.

Steps to create a static library

1. Create a C file that contains functions in your library.

2. Create a header file for the library

3. Compile library files using.

 gcc -c lib_mylib.c -o lib_mylib.o 

4. Create a static library. This step is to bundle multiple object files in one static library (see ar for details). The output of this step is the static library.

 ar rcs lib_mylib.a lib_mylib.o 

5. Now our static library is ready to use. At this point, we could just copy lib_mylib.a somewhere else to use it. For demo purposes, let us keep the library in the current directory.

No alt text provided for this image

Dynamic linking and Dynamic Libraries 

Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory. Examples of Dynamic libraries (libraries which are linked at run-time) are, .so in Linux and .dll in Windows.

Dynamic Library Creation (Linux only)

To create a Dynamic library, start by compiling your “.c” files with the following command:

gcc -c fPIC *.c

This command will take your “.c” files and return object code files ending in “.o”:

$ ls
file1.c file2.c file3.c headerfile.h
$ gcc -c fPIC *.c
file1.c file1.o file2.c file2.o file3.c file3.o headerfile.h

Now we will create our Dynamic library from these files:

gcc -shared -o libwhatever.so *.o

Our Dynamic library is now created. How can we use it?

If we want to use a program, “printhelloholberton” in our file1.c file to run a program in our main1.c file, we can compile our main1.c file in the following way:

gcc -L. main1.c -lwhatever -o printhelloholberton

By adding -L., the linker will look for a library in the current directory. Although we wrote “-lwhatever” the linker will find the library libwhatever.so and use the information found there to produce “printhelloholberton”.

Now you can add the environment variable to the library path (so it can be found):

$LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

Finally, you can run your program:

$./printhelloholberton

Program compilation: gcc -L. -lall -o my_program main.c

To view or add a comment, sign in

More articles by SNEHA DASA lakshminath

  • My journey building the web app GraphIt

    In this blog post, I would like to talk about my journey building the web app. GraphIt ~ Shopping Made Easier…

  • Postmortem

    This is a part of the Holberton School project Web Stack Debugging #3 which was released on 1st Oct 2019. We were…

  • Behind-hood of what exactly happens when you type in "www.holbertonschool.com"

    In this blog, let us talk about what happens when you type https://www.holbertonschool.

  • Behind-hood of what exactly happens when you type in https://www.holbertonschool.com

    In this blog, let us talk about what happens when you type https://www.holbertonschool.

  • Recursion

    float _pow_recursion(float x, float y) { if (y == 0) return (1); if (y < 0) return…

  • IoT (Internet Of Things)

    The internet of things, or IoT, is a system of interrelated computing devices, mechanical and digital machines…

  • MACHINE LEARNING

    Machine learning is a method of data analysis that automates analytical model building. It is a branch of artificial…

  • MUTABLE vs IMMUTABLE

    Object-oriented Programming, or OOP for short, provides a means of structuring programs so that properties and…

  • What happens when you type ls -l in your shell

    Before we jump to the answers to the above question, we need to know what is a shell, kernel, and terminal. Definition…

  • 2's COMPLEMENT

    How integers are stored in memory using two’s complement An integer is a number with no fractional part; it can be…

Explore content categories