Static vs Shared Library
https://www.glassdoor.com/Photos/Yale-University-Office-Photos-IMG205530.htm

Static vs Shared Library

Why use libraries?

In programming, a library holds functions that are compiled and ready for use. They are stored in one place for accessibility. The functions could be used and reused saving time from having to rewrite them again.

How do they work?

There are two type of libraries: static and shared (or dynamic). Both are an executable made from a collection of object files. There are a few steps taken before they become an executable. First, at the pre-processor stage the program code processes constants and macros. Next, at the compilation stage the source files are converted into object files that are machine readable. Then, at the linking stage the .o files and other shared libraries that already in the operating system are linked. Finally, at the loading stage the libraries are mapped into the program for use.

In my previous article, I wrote extensively on static library. Please check it out at the link below on how to create and use a static library.

https://www.garudax.id/pulse/build-your-own-static-library-jennifer-tang

How to create them?

First, create a header file that defines all the function names like so:

No alt text provided for this image

Next, we need to use gcc -fPIC -c *.c to compile for Position Independent Code for all the source files ending with a .c extension. It also generates relative addresses for jump calls and subroutine calls as to not use absolute addresses like in a static library. Then, use gcc -fPIC -shared *.o -o libname.so to create the shared library.

When creating a dynamic library it's obviously not already in the system. We need to use the environment variable, $LD_LIBRARY_PATH to look into other directories for the newly created dynamic library. You can export the library into the variable by LD_LIBRARY_PATH=/full/path/to/library/directory and then, export=LD_LIBRARY_PATH.

In order to check whether the library was created and stored in the variable, use ldd executable. Side note: the ldd config command creates the links and cache to other shared libraries.

The nm -D liball.so command would show a list of all the symbols used in the object file.

The difference from creating a static library is the command ar is used to create and ranlib is used to show the list of all the indexes used in the object file.

How to use them?

Use the command gcc -L main.c -lall -o a when you compile your main function file along with the shared library. The -L option will have the linker look for the shared library file in the current directory.

What are their differences?

A dynamic library is also known as a shared library. Like a static library, it collects and combines multiple object files to create one executable. The difference is during linking which is the last step in compilation of a program code. A static library takes all the object files and copies them permanently into the executable before runtime. Meanwhile, during linking of a dynamic library, it doesn't permanently copy object files into the executable. It will scan for the object files and link them at runtime.

What are their advantages and disadvantages?

No alt text provided for this image

Resources

https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html

http://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html#what_is_a_library

https://cs-fundamentals.com/c-programming/static-and-dynamic-linking-in-c.php

To view or add a comment, sign in

More articles by Jennifer Tang

  • Developing My First App: Pollen

    In my professional career, I could count on one hand the few times I jointly demonstrated leadership and technical…

    1 Comment
  • Postmortem: Wordpress 500 Incident

    Issue Summary: The Spanish comment webpage of the company’s website (hosted by Wordpress) threw a 500 Internal Sever…

  • What is recursion?

    The C programming function above calculates the result of exponentiation when given a base and exponent. In the…

  • What Happens When You Type an URL on the Browser and Press Enter

    In understanding how the web stack works and its flow, we must first understand the most basic and common term in this…

  • Internet of Things (IoT) 101

    Imagine a world controlled by devices that does not need to be operated by a human and any data collected from its…

  • Machine Learning 101

    Personally, I think everything in the world has a beginning, middle and end. Whether it is a person, a pencil or…

  • Mutable, Immutable... everything is object!

    Everything in Python is an object! It's the first thing I learned about Python and it's the most important takeaway. In…

  • What happens when you type ls -l in the shell?

    One of the most commonly used shell command is ls that displays files and directories in the current working directory.…

  • Build Your Own Static Library

    Why use them? Imagine you move into a new house with a storage closet. Your intention is to store seasonal items like…

  • Hello, World!

    The main goal of compilation is to convert high-level language to low-level language which is synonymous with C…

Explore content categories