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 lib.so add.o sub.o =>lib.so

  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 lib.so

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

$mv lib.so /usr/lib/

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

$ldconfig

  1. Now check dynamic library is linked using ldd

$ldd

  1. Now execute the program

$ ./tr

Article by-

  1. Amit Kumar

ISM Hyderabad

0
Over 15000 Students Placed
0
4000+ RECUITERS
0
YEARS OF EXPERINCE

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.