The Static library.

is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. This executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static.

The main reason to use it:

Libraries encourage the sharing of code in a modular fashion and ease the distribution of the code.

The behavior implemented by a library can be connected to the invoking program at different program lifecycle phases.

His job:

Compilation Process

Static libraries are added during the linker phase of the compilation process (above). During the linker phase: the linker links access all the libraries to link functions to the program. Static libraries are merged with other static libraries to create an executable program. During the compilation of a program, the static library is called to execute the program.

Creation:

1. Create a C file that contains functions in your library.

/* 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

/* 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.

Its use:

Let us create a driver program that uses above created static library.
1. Create a C file with main function

/* 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. -l_mylib

4. Run the driver program

./driver 
fun() called from a static library

Bibliography

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Interview Prep Day — 8

Google Cloud CDN — Best Practice

Terraform EKS Nodegroups with custom Launch Templates

Top 40 iOS Swift Questions Solutions

Data Structures in Python: Dequeue

Truecaser

URL Rewrite/Redirect for Asp.NET API and Azure Web Apps

Introduction Relational Database

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
José Miguel Petro

José Miguel Petro

More from Medium

SEVEN TIPS TO BE SUCCESSFUL We are all in a running and a moving world,where we are always trying…

HOW TO ENCOURAGE MASS CRYPTOCURRENCY ADOPTION CITING MEANFI AS A CASE STUDY AND HOW I HAVE…

CS373 Spring 2022: Varun Nayak

CS373 Spring 2022: Connor Popp Week 2