Static Vs Shared Libraries

Static Libraries vs. Dynamic Libraries: These are pre-compiled object code which is used in source code file to provide functionality for a pre-defined function or user-defined functions. There are two types of libraries widely used in Linux domain static libraries and shared libraries.

Shared Libraries: When function routines or function definitions are placed in the source code file, during compilation linker binds the function definition with source code to create expanded object code. Usually, such libraries are large in size and faster in execution. If any changes are needed then modify the actual source file & recompile it.

Functions are blocks of code that are reusable throughout a program. Using them saves time, removing the need to rewrite code multiple times. Libraries, like functions, also save time in that they make functions reusable in multiple programs.

Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.

The downside of using a static library is that its code is locked into the final executable file and cannot be modified without a re-compile. In contrast, a dynamic library can be modified without a need to re-compile.

Because dynamic libraries live outside of the executable file, the program need only make one copy of the library’s files at compile-time. Whereas using a static library means every file in your program must have its own copy of the library’s files at compile-time.

The downside of using a dynamic library is that a program is much more susceptible to breaking. If a dynamic library, for example, becomes corrupt, the executable file may no longer work. A static library, however, is untouchable because it lives inside the executable file.

The upside of using a dynamic library is that multiple running applications can use the same library without the need for each to have its own copy.

Another benefit of using static libraries is execution speed at run-time. Because it’s object code (binary) is already included in the executable file, multiple calls to functions can be handled much more quickly than a dynamic library’s code, which needs to be called from files outside of the executable.

What does this mean in practical terms? Well, imagine you’re a developer who has issued an application to thousands of users. When you want to make a few updates to the app, would you rather have to re-issue the entire program, or would you rather just issue updates in the form of modified libraries? The answer depends on the downsides your application can afford.

If you have a lot of files, multiple copies of a static library mean an increase in the executable file’s size. If, however, the benefits of execution time outweigh the need to save space, the static library is the way to go.

Steps: 1. Compile optional .c files to create an object file.

$ gcc –c add.c          $ gcc –c sub.c

  1. Link both object files to get static library

$ar –rs lib.a add.o sub.o

  1. Create object file for source code file

$ gcc –c main.c

  1. Link static library with main.o to create executable file

$gcc lib.a main.o   => ./a.out

Dynamic Libraries: When function definitions are placed in a separate file, during compilation these files are compiled as position-independent code. So, the linker binds these libraries in such a way they are not pre-loaded into an object file. But they are loaded at run time from an independent address different from the object file address.

Steps: 1. Compile optional .c files as position independent code.

$gcc –Wall –fPIC –c add.c

$gcc –Wall –fPIC –c sub.c

  1. Create shared libraries using add.o, sub.o

$gcc –shared –o add.o sub.o =>

  1. Create object file for main program

$gcc –c main.c

  1. Link shared library with object code of main

$gcc –o tr main.o

  1. Move shared library (.so) to standard path /usr/lib/

$mv /usr/lib/

  1. Run “ldconfig” to link & load dynamic library from standard path


  1. Now check dynamic library is linked using ldd


  1. Now execute the program

$ ./tr

Article by-

  1. Amit Kumar

ISM Hyderabad

#Embedded Courses in Bangalore #Embedded Courses in Hyderabad #Embedded Jobs for Freshers #Embedded C #Embedded Systems #Embedded Training in Hyderabad #Embedded Classes in Hyderabad #Embedded Systems Course in Hyderabad #Embedded Programming Courses in Hyderabad #Embedded Training in Bangalore #Embedded Classes in Bangalore #Embedded Systems Course in Bangalore #Embedded Programming Courses in Bangalore #Best Embedded Training Institute in Bangalore #Best Embedded Training Institute in Hyderabad ​#Embedded Training Institute in Bangalore ​#Embedded Training Institute in Hyderabad #Best Embedded Course in Bangalore #Best Embedded Course in Hyderabad #best iot training institute in Bangalore #best iot training institute in Hyderabad #iot training institute in bangalore #iot training institute in Hyderabad #best iot course in Bangalore #best iot course in hyderabad #Embedded Jobs in Bangalore #Embedded Jobs in Hyderabad #Iot jobs in Bangalore #IoT jobs in Hyderabad #Python Jobs in Bangalore #Python Jobs in Hyderabad #AIML #Artificial Intelligence #machine learning #Data Science Courses in Bangalore #Data Science Courses in Hyderabad #Data Science Courses #Best Data Science Training Institute #Best Data science Course #Best Python Course #Best Iot Course #Best Iot Training Institute in Bangalore #Best Iot Training Institute in Hyderabad #Iot Certification Course Bangalore #Iot Certification Course Hyderabad #Best Artificial Intelligence Training Institute Bangalore #Best Artificial Intelligence Training Institute Hyderabad #Best fullstack Training Institute in Bangalore #Best full stack Training Institute in Hyderabad ​#full stack Training Institute in Bangalore #full stack Training Institute in Hyderabad #Best full stack Course in Bangalore #Best full stack Course in Hyderabad


useful links