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,
files in Linux and
files in Windows.
Steps to create a static library
Let us create and use a Static Library in UNIX or UNIX like OS.
Create a C file that contains functions in your library.
("fun() called from a
We have created only one file for simplicity. We can also create multiple files in a library.
Create a header file for the library
Compile library files.
gcc -c lib_mylib.c -o lib_mylib.o
Create static library. This step is to bundle multiple object files in one static library (see
for details). The output of this step is static library.
ar rcs lib_mylib.a lib_mylib.o
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
Create a C file with main function
Compile the driver program.
gcc -c driver.c -o driver.o
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
for details of -L and -l options).
gcc -o driver driver.o -L. lib_mylib.a
Run the driver program
fun() called from a static library
Following are some important points about static libraries.
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.
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.
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.
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.
One drawback of static libraries is, for any change(up-gradation) in the static libraries, you have to recompile the main program every time.
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,
in Linux and
in Windows. We will soon be covering more points on Dynamic Libraries and steps to create them. This article is compiled by
and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Unlock the Power of Placement Preparation!
Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete Interview Preparation
Course is the ultimate guide to conquer placements. Trusted by over 100,000+ geeks, this course is your roadmap to interview triumph.
Ready to dive in? Explore our Free Demo Content and join our Complete Interview Preparation