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.

Subscribe to enable complete access to all , and .

This article will help you to gain required knowledge to successfully complete , which are required to complete and earn course completion certificates 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

You need to subscribe & logged in

Subscribe

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

 Articles

 Linux Software

 C Programming

 Search Code Snippets

 Popular C search examples

 Popular C search MCQs

 Test your skills:online tests

 C Programming

 Python Programming

 Linux Software

 Quantitative Aptitude

 Embedded System Software