Advertisements


Description
    In this article I will show you how to create shared library with GCC compiler in linux with an example project and test the shared library. We will develop the shared library for a simple mini calculator project. We will follow the following steps in this article.
    I have executed the source code using a raspberry pi. You can buy a low cost Raspberry Pi Zero WH (Zero W with Headers) or CanaKit Raspberry Pi 3 B+ (B Plus) Starter Kit (32 GB EVO+ Edition, Premium Black Case) or EleDuino BeagleBone Black Rev C (4G) Single Board Computer Development Board to execute the programs listed on this article. You can also install linux on windows using virtualbox simulation environment to execute programs used in this article.

Source code files creation and function definitions

File add.c and function add definition.
    Let's create a source file named add.c and write a function named add to take two integers as arguments and return the result after adding the integers passed to it.

int add(int a,int b)
{
	return a + b;
}
File subtract.c and function subtract definition.
    Let's create a source file named subtract.c and write a function named subtract to take two integers as arguments and return the result after subtracting the second integer from the first integer.

int subtract(int a,int b)
{
	return a - b;
}
File multiply.c and function multiply definition.
    Let's create a source file named multiply.c and write a function named multiply to take two integers as arguments and return the result after multiplying the first integer with the second integer.

int multiply(int i1, int i2) {
	return i1 * i2;
}
File divide.c and function divide definition.
    Let's create a source file named divide.c and write a function named divide to take two integers as arguments and return the result after dividing the second integer with the first integer.

int divide(int i1, int i2) {
	return i1 / i2;
}

Common header file and function prototypes

File common.h and function prototypes.
    Let's create a header file named common.h and write the prototypes for functions add, subtract, multiply, divide.

int add(int, int);
int subtract(int, int);
int multiply(int , int );
int divide(int ,int );

Compiling source files to make memory position independent

Compiling source files.
    Let's compile the library source code files add.c, subtract.c, multiply.c, divide.c to make memory position independent.

$ gcc -c -Wall -Werror -fpic add.c subtract.c multiply.c divide.c
$ ls
add.c  common.h  divide.o    multiply.c  subtract.c
add.o  divide.c  libcalc.so  multiply.o  subtract.o
$
Creating a shared library from an object(.o) file.
    In this step we will change the object files into a shared library and name the shared library as libcalc.so.

$ gcc -shared -o libcalc.so add.o subtract.o multiply.o divide.o
$ ls
add.c  common.h  divide.o    multiply.c  subtract.c
add.o  divide.c  libcalc.so  multiply.o  subtract.o
$

File main.c file creation and testing shared library libcalc functions

Calling library functions from main function.
    Let's create a main.c file and call add, subtract, multiply, divide functions from it.

#include <stdio.h>
#include "common.h"
int main()
{
	int i1 = 20,i2 = 10,res;
	res = add(i1,i2);
	printf("Add result:%d\n",res);
	res = subtract(i1,i2);
	printf("Subtract result:%d\n",res);
	res = multiply(i1,i2);
	printf("Multiplication result:%d\n",res);
	res = divide(i1,i2);
	printf("Division result:%d\n",res);	
	return 0;
}

Linking with a shared library

Linking shared library.
    Let's link the shared library libcalc.so we have created previously with the main.c file so that we can call the functions inside the shared library and get their response. Also we name the executable as Calculator. We need to explicitly specify the location of the shared library libcalc.so with a -L option. Otherwise gcc will serach for it by default in the file system, will not get it and linking process will fail.

$ pwd
/home/pi/C
$ gcc -L/home/pi/C -Wall -o Calculator main.c -lcalc
$ ls
add.c  Calculator  divide.c  libcalc.so  multiply.c  subtract.c
add.o  common.h    divide.o  main.c      multiply.o  subtract.o
$

Copy shared library to /usr/lib

Copy library to usr/lib or /usr/local/lib for access.

$ sudo cp libcalc.so /usr/lib/
$

Executing the Calculator executable file

Executing the Calculator executable file.

$ ./Calculator
Add result:30
Subtract result:10
Multiplication result:200
Division result:2
$

Conclusion

Article conclusion.
    In this article we have successfully created a shared library libcalc.so for a simple mini calculator project and tested all the library functions from main.c application.


Want to contribute or ask a new article? Write and upload your article information .

Share

Articles


Linux software

More Articles Categories

Technical articles

Prepare for software jobs


Test your skills