Shared library development with gcc in linux

In:
Share

Article Background

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. I have executed the source code using a raspberry pi. You can also install linux on windows using virtualbox simulation environment to execute programs used in this article.

Online courses related to this article


Course description

Learn to develop software solutions for linux environment, implement requirements through real time projects and get required practical skills for software jobs



Course description

Learn to develop embedded systems, interfacing electronic peripherals through real time projects and get required practical skills for software jobs

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.

To Read Full Article

Start any course from the following

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