Static Library

Static Library

·

4 min read

printf() is a commonly known function used to display text on the screen. It belongs to the C library functions that perform input-output operations located in stdio.h file. At the linking stage of the compilation proccess the function is added dynamically to the program that uses it, to its executable file.

A library is a file that contains functions that can be used by other programs.

It consists of two parts

  • header file: it is a file with .h file extension that contains function declarations, macros. It is included in a program by using #include preprocessor directive
  • a library containing implementations of the functions : These files are compiled and made available in binary form.

A library file is not executable but instead linked (added) to an executable file by a linker. The file name begins with lib and the file extension tells whether it is a static library (.a) or a shared library (.so).

Types of Libraries

There are two types of libraries:-

Static library

A static library is utilized during compile time. The library file has to be available in a location so that the linker can copy the called function code to the executable file in a process referred as static linking. The function code exists as object files that are turned into libraries by an archive.

The command ar is used to create a static library.

Drawbacks of Static Library

  1. Increased size: A program that uses many functions from the static library will increase in size since the functions are copied to the executable file. in compilation,
  2. In the case of modification, the changes do not reflect on the end application. The program requires to be compiled again so that the updated function code is linked (added) to the executable program.

Shared Library

A dynamic library is utilized during runtime. At the linking stage during the compilation of a program, the library is linked to a program executable file through dynamic linking.

The program references the library by its memory address (where the function code is implemented) and the address is added to the final executable file. Thus, this leads to a decreased program file. In case of changes in the dynamic libraries, it is automatically applied to the program.

How to create a static library

For this example, I am creating a static library called libcalc.a which contains basic arithmetic operations.

  1. Create a main.h header file and include the function prototypes.

  2. Create .c files implementing the declared functions in the header file.

  3. Generate the object code of the functions

    gcc -c *.c
    

This command compiles the .c files and generates object files.

4.Create a static library

ar -rc libcalc.a *.o

This command adds all the object files generated during compilation to the static library libcalc.a

  • ar - the command to create a static library and stores the files in an archive

  • -rc - gives an option such that if the file exists it should be replaced (r) and if the file does not exist it should be created (c).

5.Ran ranlib to index the archive

 ranlib libcalc.a

-Display the object files in the static library using the below command

ar -t libcalc.a

6.Use nm to list the symbols stored in a static library

nm libcalc.a

How to use the static library

#include "main.h"

int main()
{
int a = 8, b = 10;
add(a, b);
return 0;
}

In this program, I am using add function which is declared in the main.h

In the command line run

gcc main.c -L. -lcalc -o main

  • gcc is the name of the compiler

  • main.c is the name of the program

  • -L indicates that the static library is in the current directory

  • lcalc is the name of the library without lib and the .a extension

  • main is the name of the executable file

The returned value after executing the file will be 18.

This is how to create a static library and I hope the information has been helpful to you.