Dynamic vs Static Libraries

The compilation process has four steps:

> Pre-processor phase > Compiler phase > Assembler phase > Linker phase.

If you want to look a little deeper about this, please check this link: Compilation process of a C Code.

Static Libraries:

HowTo — Static library [video] — https://www.youtube.com/watch?v=eW5he5uFBNM

How to use Static Libraries:

Dynamic or Shared Libraries:

  • During compilation time, the linker verifies that all symbols (functions, variables, etc) that the program requires exists in the shared libraries.
  • When the program is executed, there is a program called ‘Dynamic loader’ checks which shared libraries were linked previously, then load them in the memory, and attaches them the copy of the program loaded in the memory.
HowTo — Static library [video] — https://www.youtube.com/watch?v=eW5he5uFBNM

How to use Dynamic Libraries:

  • PIC of Position Independent Code:
    As we don’t know where the objects files in the library will be inserted in the memory and different programs can use them, we need relative addresses of the objects, we can do that in most Linux systems with gcc fPIC -c [objectfiles] command. In the image below, you can see that using the command will create the objects files.
Creating objects files.
  • Shared Library Creation:
    The dynamics libraries are not the final executable file, but a file in a format that depends on the specific architecture for which it is being created. For most Linux operating systems we do this with the command:
Creating library file
checking liball.so
  • Using Shared Library:
ldd liball.so

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store