Creating C/C++ library for Linux

Tanishq Rawat
3 min readNov 10, 2021

--

In this blog, I will demonstrate creating library for Linux. In learning phase we create many functions, structures, classes which can help us in future task’s also. Such as we need to find whether a string is palindromic or not, so why to write this code again, just create a function in library and call it.

Note-: We will create static library for our use and will create a library to compute addition, subtraction, multiplication and division of two numbers.

First of all you should create a folder structure for better understanding

Create this folder structure

Now stay in INCLUDE directory to create the prototype (header file) for your library.

// tanishqHeader.h
// this file will contain the prototype of our source file
int getSum(int,int); //to add two numbers
int getDiff(int,int); //to subtract two numbers
int getMul(int,int); //to multiply two numbers
int getDiv(int,int); //to divide two numbers

Now stay in SRC directory to create the source code for your library.

// tanishqSource.c
// this file will contain the source code
#include<tanishqHeader.h>
int getSum(int a,int b)
{
return a+b;
}
int getDiff(int a,int b)
{
return a-b;
}
int getMul(int a,int b)
{
return a*b;
}
int getDiv(int a,int b)
{
return a/b;
}

For compiling this source file

gcc -c tanishqSource.c -I ../include //this will generate .o file (object file)ar rcs libtanishq.a tanishqSource.o//this will generate the library file named as libtanishq.a, having //lib as prefix is the rule written in documentation. mv libtanishq.a ../lib//this will move to library file to lib folder

For testing the generated library we will now move to testcases folder and will create a testcase.c file

//testcase.c
#include<tanishqHeader.h>
#include<stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("Sum of %d and %d is: %d\n",a,b,getSum(a,b));
printf("Difference of %d and %d is: %d\n",a,b,getDiff(a,b));
printf("Multiplication of %d and %d is: %d\n",a,b,getMul(a,b));
printf("Division of %d and %d is: %d\n",a,b,getDiv(a,b));
return 0;
}

For compiling and executing the output file.

gcc -static testcase.c -o testcase.out -I ../include -L ../lib -ltanishq
// here tanishq is the name of the library file
// using -ltanishq we are saying that gcc should include this library file also while compiling the testcase.c file.
contents of tanishqHeader.h
Contents of Source file and commands to compile and create library file
Testcase file, command to compile the testcase file and the output

This demonstration was based on C language, if you are using C++ then there might be small changes as extension of header file here was .h in C++ there will be no extension include folder will contain the file named as tanishqHeader only. While compiling we will use g++ and you can create your own namespace which will contain various classes.

--

--

Tanishq Rawat
Tanishq Rawat

Written by Tanishq Rawat

SDE @ Mobileum | Ex-SDE Intern at Cerebry

No responses yet