Static and Dynamic Libraries | Set 1
Last Updated :
07 Aug, 2023
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
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 funct
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.
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
4 min read
Difference Between Static andDynamic Loading in Operating System
Static loading means loading the entire program and all necessary libraries into memory before starting execution. Dynamic loading loads only the parts of the program such as specific functions or libraries when they are needed during execution.Static Loading in OS In static loading, the entire prog
5 min read
Difference between Static and Dynamic Memory Allocation in C
In C++, memory allocation is a process by which computer programs and services are assigned 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 o
3 min read
Address Binding and its Types
Address Binding is the mapping of a physical address to a logical address known as a virtual address, it allocates a physical memory region to a logical pointer. In this article, We are going to cover address binding with the help of an example and Its types like compile time, load time, and executi
5 min read
Difference between Loading and Linking in Operating System
An operating system acts as an intermediary between the user of a computer and computer hardware. The purpose of an operating system is to provide an environment in which a user can execute programs conveniently and efficiently. Linking and Loading are utility programs that play an important role in
4 min read
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 stati
5 min read
Difference between Header file and Library
Header Files: The files that tell the compiler how to call some functionality (without knowing how the functionality actually works) are called header files. They contain the function prototypes. They also contain Data types and constants used with the libraries. We use #include to use these header
3 min read
How to Create a Static Library in C++?
C++ allows users to add their own libraries to reduce the amount of code we have to write in our program. A static library in C++ is a library that is linked to the program at compile-time. In this article, we will learn how to create a static library in C++ from scratch. Create Static Library in C+
3 min read