DEV Community

Cover image for Introduction to static libraries in C programming language.
Danstan-O
Danstan-O

Posted on

Introduction to static libraries in C programming language.

WHAT IS A STATIC LIBRARY

A library is a collection of already build functions, variables, classes and anything that should make your code run. You can use the code in your program without knowing how it's declared.
Static libraries are a collection of object files that are linked into the program during the linking phase.

HOW TO CREATE A STATIC LIBRARY

We first need to create a normal c file. After creating the c file define all the functions needed in that file.
After our functions have been defined we create a header file that will be used to store all the prototypes that will be needed in our program.
The header file should have a .h file extension.
The following code should be included in our main.h file.

#ifndef MAIN_H
#define MAIN_H


#endif
Enter fullscreen mode Exit fullscreen mode

A WALK THROUGH EXAMPLE

I will create 2 c files and use them to demonstate the concepts described above. The file names will be monalisa.c and sum.c. We also have to create the main.c file which will act as our main entry point to the program.
The monalisa.c file will have the following code.

#include <stdio.h>
#include "main.h"

/**
 * monalisa - file returns a print statement
 * Return: returns 0
 */

void monalisa(void)
{
printf("Welcome home, Kid\n");
}

Enter fullscreen mode Exit fullscreen mode

The sum.c file can have the following snippet

#include <stdio.h>
#include "main.h"

/**
 * sum - adds up two integers
 * @z: our first input value
 * @y: our second input value
 * Return: returns the sum of two integers
 */

int sum(int y, int z)
{
int sum;
sum = y + z;
printf("The sum of the two integers is: %d\n", sum);
return (sum);
}

Enter fullscreen mode Exit fullscreen mode

our main.c file can have the following piece of code

#include "main.h"

/**
 * main - the major entry point
 * Return: it returns a 0
 */

int main(void)
{
monalisa();
sum(1, 2);
return (0);
}

Enter fullscreen mode Exit fullscreen mode

and finally our main.h file will have the following piece of code that will contain our prototypes and will be used to call the monalisa and sum functions.

#ifndef MAIN_H
#define MAIN_H

void monalisa(void);
int sum(int y, int z);


#endif

Enter fullscreen mode Exit fullscreen mode

Running the command 'gcc -c *.c' generates all the .o files from the .c files that will be in the current directory.

We can then create a library named lib.a with the command
'ar rcs lib.a'

After creating the library we run the command 'ar rcs lib.a *.o' which helps move copies of every .o files into our library.
Note: The s flag tells the ar command to add an index to the archive or update it if the index already exists.
This can also be achieved by running the command 'ranlib lib.a' after generating the .o files.

After indexing our library we can run the commandn 'ar -t lib.a' to see if our file is indexed properly.
Finally we run the ' nm lib.a' command to list all the symbols of the files that we have and from this we can see the files that we were unable to link into our object file.

TOP TIP

We can create an executable file script that can be used to automate the process of generating .o library files.
Create a file named static_lib.sh and put the following code in it.

#!/bin/bash
gcc -c *.c
ar rc bilan.a *.o
ranlib bilan.a
Enter fullscreen mode Exit fullscreen mode

After writing the code you can make the file executable by running the following command.

chmod u+x static_lib.sh

The above script creates a static library called bilan.a from all the .c files that are in our current directory.

After creating an executable file we run the following commmand on the terminal to test our executable and add all the .o files that will have been generated from the .c files into our library named bilan.a
./create_static_lib.sh

All the libaries have a .a exentionj to them.

By Oduor.

Top comments (0)