Difference Between Static And Shared Libraries In C

Libraries are essential in the realm of software development, offering reusable code components that streamline the programming workflow. They are generally classified into two main categories: static and shared libraries. Much like experienced explorers exchanging maps and resources, these libraries provide valuable functionalities to programs.

Static Libraries:

Static libraries, also known as archive libraries, are groups of object files that are linked with a program at the time of compilation. During the compilation process, the essential functions from the library are integrated into the final executable file. This allows the compiled program to function autonomously without the need for external libraries when it is executed.

To generate a static library, developers compile separate source code files into object files and then employ the archiving tool to combine them into a unified archive file. Subsequently, the linker associates this archive file with the primary program while compiling.

Shared libraries:

Dynamic or shared object libraries, also referred to as shared libraries, are loaded while the program is running, rather than being included in the executable during compilation. This dynamic loading mechanism enables programs to share the library, thereby decreasing memory consumption across multiple programs.

Shared libraries are saved as dynamic link libraries on Windows operating systems and as shared objects on Unix-like systems. These files hold precompiled code that is loaded into memory either at the start of a program or on demand during its execution.

Example:

Let's consider a program that demonstrates the process of creating and utilizing static libraries in the C programming language.

Creating source files for the static library

File1: math_operaations.h

Example

#ifndef MATH_OPERATIONS_H

#define MATH_OPERATIONS_H

double add(double a, double b);

double subtract(double a, double b);

#endif

File2: math_operations.c

Example

#include "math_operations.h"

double add(double a, double b) {

 return a + b;

}

double subtract(double a, double b) {

 return a - b;

}

Source files creation:

The "mathoperations.h" header file contains the function prototypes for addition and subtraction, which will be implemented in the static library. The "mathoperations.o" source file contains the actual implementations of these functions as declared in "math_operations.h".

Compile the source files into a static library

Begin by launching the terminal application and moving to the folder that houses the source files.

Example

gcc -c math_operations.c -o math_operations.o

ar rcs libmathstatic.a math_operations.o

The file named "math_operations.c" undergoes compilation into an object file by executing the gcc-c command. This process generates machine code tailored to the particular architecture without generating the ultimate executable file.

Subsequently, the compiled object file is bundled into a static library named "libmathstatic.a" through the "ar rcs" command. This static library houses the implemented functions.

Example:

Let's consider a C program that makes use of a static library:

Example

#include <stdio.h>

#include "math_operations.h"

int main() {

 double num1 = 10.0, num2 = 5.0;

 double sum_result = add(num1, num2);

 printf("Sum: %f\n", sum_result);

 double diff_result = subtract(num1, num2);

 printf("Difference: %f\n", diff_result);

 return 0;

}

Compile the main program with static library

Example

gcc main.c -o main -L. -lmathstatic

then run the compiled program

./main

The "main.c" source code file is transformed into an object file through the "gcc" compiler.

The "gcc" linker is employed to connect the object file with the static library utilizing the -L library path.

Final execution:

The individual initiates the compiled binary in the command line interface. The software runs and carries out its operations, making use of the functionalities provided by the static library for performing mathematical computations.

Differences between the static and shared libraries:

There exist several variances between static and shared libraries in the C programming language. Here are some key variations between static and shared libraries:

Features Static library Shared libray
Compilation Time Linked at compile time Linked at compile time (symbolic references) or runtime (actual addresses)
File Extension .a (Archive file) .so (Shared Object) or .dll (Dynamic Link Library)
Size Larger executable size Smaller executable size
Memory Usage Higher memory consumption as code is duplicated Lower memory consumption as code is shared
Update Requires recompilation for library changes Dynamic updates without recompilation
Dependency Independent of external libraries during runtime Dynamic linking introduces runtime dependency
Loading Time Faster loading time as everything is included in the executable Slightly slower loading time due to dynamic linking
Flexibility Less flexible as changes require recompilation More flexible as changes can be made without recompilation
Symbol Resolution Resolved at compile time Resolved at compile time (symbolic references) or runtime (actual addresses)
Isolation Code and data are isolated within the executable Code is shared among multiple processes
Execution Speed Potentially faster due to local optimizations It may have a slight overhead due to dynamic linking.
Distribution A larger executable size may affect the distribution. A smaller executable size is favorable for distribution.
Linking Type Static linking Dynamic linking
Platform Compatibility More portable as it includes the necessary code Requires compatible shared library versions
Debugging Easier debugging as all code is available Debugging may be more challenging due to shared code

Conclusion:

The decision on whether to use static or shared libraries is influenced by project needs, performance factors, distribution limitations, and the level of adaptability desired. Both library types play a crucial role in the varied realm of software creation, and comprehending their intricacies enables developers to make well-informed choices that align with their individual requirements.

Input Required

This code uses input(). Please provide values below: