The Wayback Machine - https://web.archive.org/web/20240912140456/https://www.geeksforgeeks.org/static-vs-dynamic-libraries/
Open In App

Static and Dynamic Libraries | Set 1

Last Updated : 07 Aug, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

When a C program is compiled, the compiler generates object code. After generating the object code, the compiler also invokes linker. One of the main tasks for linker is to make code of library functions (eg printf(), scanf(), sqrt(), ..etc) available to your program.

A linker can accomplish this task in two ways, by copying the code of library function to your object code, or by making some arrangements so that the complete code of library functions is not copied, but made available at run-time.

Static Linking and Static Libraries

is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory. Examples of static libraries (libraries which are statically linked) are,

.a

files in Linux and

.lib

files in Windows.

Steps to create a static library

Let us create and use a Static Library in UNIX or UNIX like OS.

1.

Create a C file that contains functions in your library.

C




/* Filename: lib_mylib.c */
#include <stdio.h>
void fun(void)
{
  printf("fun() called from a static library");
}


We have created only one file for simplicity. We can also create multiple files in a library.

2.

Create a header file for the library

C




/* Filename: lib_mylib.h */
void fun(void);


3.

Compile library files.

 gcc -c lib_mylib.c -o lib_mylib.o 

4.

Create static library. This step is to bundle multiple object files in one static library (see

ar

for details). The output of this step is static library.

 ar rcs lib_mylib.a lib_mylib.o 

5.

Now our static library is ready to use. At this point we could just copy lib_mylib.a somewhere else to use it. For demo purposes, let us keep the library in the current directory.

Let us create a driver program that uses above created static library

.

1.

Create a C file with main function

C




/* filename: driver.c  */
#include "lib_mylib.h"
void main()
{
  fun();
}


2.

Compile the driver program.

gcc -c driver.c -o driver.o

3.

Link the compiled driver program to the static library. Note that -L

.

is used to tell that the static library is in current folder (See

this

for details of -L and -l options).

gcc -o driver driver.o -L. lib_mylib.a

4.

Run the driver program

./driver 
fun() called from a static library

Following are some important points about static libraries.

1.

For a static library, the actual code is extracted from the library by the linker and used to build the final executable at the point you compile/build your application.

2.

Each process gets its own copy of the code and data. Where as in case of dynamic libraries it is only code shared, data is specific to each process. For static libraries memory footprints are larger. For example, if all the window system tools were statically linked, several tens of megabytes of RAM would be wasted for a typical user, and the user would be slowed down by a lot of paging.

3.

Since library code is connected at compile time, the final executable has no dependencies on the library at run time i.e. no additional run-time loading costs, it means that you don’t need to carry along a copy of the library that is being used and you have everything under your control and there is no dependency.

4.

In static libraries, once everything is bundled into your application, you don’t have to worry that the client will have the right library (and version) available on their system.

5.

One drawback of static libraries is, for any change(up-gradation) in the static libraries, you have to recompile the main program every time.

6.

One major advantage of static libraries being preferred even now “is speed”. There will be no dynamic querying of symbols in static libraries. Many production line software use static libraries even today.

Dynamic linking and Dynamic Libraries

Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory. Examples of Dynamic libraries (libraries which are linked at run-time) are,

.so

in Linux and

.dll

in Windows. We will soon be covering more points on Dynamic Libraries and steps to create them. This article is compiled by

Abhijit Saha

and reviewed by GeeksforGeeks team.



Similar Reads

Understanding storage of static methods and static variables in Java
In every programming language, memory is a vital resource and is also scarce. Hence the memory must be managed thoroughly without any leaks. Allocation and deallocation of memory is a critical task and requires a lot of care and consideration. In this article, we will understand the storage of static methods and static variables in Java. Java Virtu
6 min read
Static and Dynamic Linking in Operating Systems
Static Linking: When we click the .exe (executable) file of the program and it starts running, all the necessary contents of the binary file have been loaded into the process's virtual address space. However, most programs also need to run functions from the system libraries, and these library functions also need to be loaded. In the simplest case,
3 min read
Static and Dynamic Loader in Operating System
An Operating system is the critical component for managing the various memory resources in the computer system. One of the essential tasks of the operating system is to manage the executable programs or applications in the computer system. A program needs to be loaded into the memory for the purpose of execution. For the execution context, the load
7 min read
Difference between Static and Dynamic Memory Allocation in C
Memory Allocation: Memory allocation is a process by which computer programs and services are assigned with physical or virtual memory space. The memory allocation is done either before or at the time of program execution. There are two types of memory allocations: Compile-time or Static Memory AllocationRun-time or Dynamic Memory AllocationStatic
3 min read
Working with Shared Libraries | Set 1
This article is not for those algo geeks. If you are interested in systems related stuff, just read on... Shared libraries are useful in sharing code which is common across many applications. For example, it is more economic to pack all the code related to TCP/IP implementation in a shared library. However, data can't be shared as every application
5 min read
Working with Shared Libraries | Set 2
We have covered basic information about shared libraries in the previous post. In the current article, we will learn how to create shared libraries on Linux. Prior to that, we need to understand how a program is loaded into memory, and the various (basic) steps involved in the process. Let us see a typical "Hello World" program in C. Simple Hello W
4 min read
Thread Libraries
The Operating System is an interface between the user and hardware that enables the interaction of computer hardware and software or it can act as a bridge between the user and the application. It ensures that various software programs can run efficiently and interact with hardware as needed. Some Popular Operating Systems are Linux, Unix, Microsof
6 min read
Difference Between Static andDynamic Loading in Operating System
An operating system is a program loaded into a system or computer. and manage all the other program which is running on that OS Program, it manages the all other application programs. or in other words, we can say that the OS is an interface between the user and computer hardware. Static Loading in OS In static loading, Initially, the complete prog
5 min read
Fixed (or static) Partitioning in Operating System
Fixed partitioning, also known as static partitioning, is a memory allocation technique used in operating systems to divide the physical memory into fixed-size partitions or regions, each assigned to a specific process or user. Each partition is typically allocated at system boot time and remains dedicated to a specific process until it terminates
6 min read
Difference Between Basic Disk and Dynamic Disk
When managing storage in Windows operating systems, you might come across two primary kinds of disk configurations: Basic Disk and Dynamic Disk. To do this well and make sure that your data is organized according to your needs, it becomes important to know the differentiation between these disk types. This article will explore the main features and
6 min read
Priority to Round-robin scheduling with dynamic time quantum
Prerequisite - Program for Round Robin scheduling Processes are executed on basis of their priority in this scheduling algorithm. Processes have a specific priority associated with them on arrival. When a process arrives its priority is compared with other processes in the ready queue and if its priority is higher then it becomes running process. I
2 min read
Variable (or Dynamic) Partitioning in Operating System
In operating systems, Memory Management is the function responsible for allocating and managing a computer’s main memory. The memory Management function keeps track of the status of each memory location, either allocated or free to ensure effective and efficient use of Primary Memory. Below are Memory Management Techniques. ContiguousNon-Contiguous
4 min read
Hardware Synchronization Algorithms : Unlock and Lock, Test and Set, Swap
Process Synchronization problems occur when two processes running concurrently share the same data or same variable. The value of that variable may not be updated correctly before its being used by a second process. Such a condition is known as Race Around Condition. There are a software as well as hardware solutions to this problem. In this articl
5 min read
Get the stack size and set the stack size of thread attribute in C
Prerequisite : Multithreading Syntax : // to get size of stack int pthread_attr_getstacksize(const pthread_attr_t* restrict attr, size_t* restrict stacksize); // to set size of stack int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stacksize); . pthread_attr_getstacksize() : It is use for get threads stack size. The stacksize attribute gi
2 min read
Readers-Writers Problem | Set 1 (Introduction and Readers Preference Solution)
The readers-writer problem in operating systems is about managing access to shared data. It allows multiple readers to read data at the same time without issues but ensures that only one writer can write at a time, and no one can read while writing is happening. This helps prevent data corruption and ensures smooth operation in multi-user systems.
8 min read
Operating Systems | Set 2
Following questions have been asked in GATE CS exam. 1. Consider a machine with 64 MB physical memory and a 32-bit virtual address space. If the page size is 4KB, what is the approximate size of the page table? (GATE 2001) (a) 16 MB (b) 8 MB (c) 2 MB (d) 24 MB Answer: (c) Explanation: A page entry is used to get address of physical memory. Here we
4 min read
Operating Systems | Set 4
Following questions have been asked in GATE CS exam. 1. Using a larger block size in a fixed block size file system leads to (GATE CS 2003) a) better disk throughput but poorer disk space utilization b) better disk throughput and better disk space utilization c) poorer disk throughput but better disk space utilization d) poorer disk throughput and
3 min read
Operating Systems | Set 5
Following questions have been asked in GATE 2012 exam. 1. A process executes the code fork (); fork (); fork (); The total number of child processes created is (A) 3 (B) 4 (C) 7 (D) 8 Answer (C) Let us put some label names for the three lines fork (); // Line 1 fork (); // Line 2 fork (); // Line 3 L1 // There will be 1 child process created by lin
3 min read
Operating Systems | Set 6
Following questions have been asked in GATE 2011 CS exam. 1) A thread is usually defined as a 'light weight process' because an operating system (OS) maintains smaller data structures for a thread than for a process. In relation to this, which of the followings is TRUE? (A) On per-thread basis, the OS maintains only CPU register state (B) The OS do
4 min read
Operating Systems | Set 10
Following questions have been asked in GATE 2008 CS exam. 1) The data blocks of a very large file in the Unix file system are allocated using (A) contiguous allocation (B) linked allocation (C) indexed allocation (D) an extension of indexed allocation Answer (D) The Unix file system uses an extension of indexed allocation. It uses direct blocks, si
3 min read
Operating Systems | Set 7
Following questions have been asked in GATE CS exam. 1) Let the time taken to switch between user and kernel modes of execution be t1 while the time taken to switch between two processes be t2. Which of the following is TRUE? (GATE CS 2011) (A) t1 > t2 (B) t1 = t2 (C) t1 2) A system uses FIFO policy for page replacement. It has 4 page frames with n
3 min read
Operating Systems | Set 8
Following questions have been asked in GATE 2009 CS exam. 1) In which one of the following page replacement policies, Belady’s anomaly may occur? (A) FIFO (B) Optimal (C) LRU (D) MRU Answer (A) Belady's anomaly proves that it is possible to have more page faults when increasing the number of page frames while using the First in First Out (FIFO) pag
3 min read
Operating Systems | Set 9
Following questions have been asked in GATE 2009 CS exam. 1) In the following process state transition diagram for a uniprocessor system, assume that there are always some processes in the ready state: Now consider the following statements: I. If a process makes a transition D, it would result in another process making transition A immediately. II.
3 min read
Operating Systems | Set 11
Following questions have been asked in GATE 2008 CS exam. 1) A process executes the following code for (i = 0; i The total number of child processes created is (A) n (B) 2^n - 1 (C) 2^n (D) 2^(n+1) - 1; Answer (B) F0 // There will be 1 child process created by first fork / \ F1 F1 // There will be 2 child processes created by second fork / \ / \ F2
3 min read
Operating Systems | Set 12
Following questions have been asked in GATE CS 2007 exam. 1) Consider a disk pack with 16 surfaces, 128 tracks per surface and 256 sectors per track. 512 bytes of data are stored in a bit serial manner in a sector. The capacity of the disk pack and the number of bits required to specify a particular sector in the disk are respectively: (A) 256 Mbyt
3 min read
Operating Systems | Set 13
Following questions have been asked in GATE CS 2007 exam. 1) A virtual memory system uses First In First Out (FIFO) page replacement policy and allocates a fixed number of frames to a process. Consider the following statements: P: Increasing the number of page frames allocated to a process sometimes increases the page fault rate. Q: Some programs d
4 min read
Operating Systems | Set 14
Following questions have been asked in GATE CS 2006 exam. 1) Consider three CPU-intensive processes, which require 10, 20 and 30 time units and arrive at times 0, 2 and 6, respectively. How many context switches are needed if the operating system implements a shortest remaining time first scheduling algorithm? Do not count the context switches at t
3 min read
Operating Systems | Set 15
Following questions have been asked in GATE CS 2006 exam. 1) Consider three processes (process id 0, 1, 2 respectively) with compute time bursts 2, 4 and 8 time units. All processes arrive at time zero. Consider the longest remaining time first (LRTF) scheduling algorithm. In LRTF ties are broken by giving priority to the process with the lowest pr
4 min read
Operating Systems | Set 16
Following questions have been asked in GATE CS 2005 exam. 1) Normally user programs are prevented from handling I/O directly by I/O instructions in them. For CPUs having explicit I/O instructions, such I/O protection is ensured by having the I/O instructions privileged. In a CPU with memory mapped I/O, there is no explicit I/O instruction. Which on
4 min read
Operating Systems | Set 17
Following question has been asked in GATE 2012 CS exam. Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value of memory location X, increments it by the value i, and returns the old value of X. It is used in the pseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variable initialized to
2 min read