Creating Shared Library In Linux

In this tutorial of creating shared library and using with your source code, let us assume that

  1. Shared library is to be created for the function library.cpp
  2. The name for the shared library is liblibrary.so
  3. Shared library is going to be executed with main.cpp

Let us see whats their in library.cpp

include "library.hpp"
#include  <iostream>
using namespace std;
void value_to_be_printed( int x )
{
    cout<<"inside  shared library"<<endl;
    cout << x << "\n";
}        

This source code consist of the a function value_to_be_printed Which is just printing a integer value which is passed to it as an argument during function call.

It also consist of a header file library,hpp lets see that too(click_here). It just consist of defination of the value_to_be_printed

After creating a shared library which consist of defination of value_to_be_printed its going to link with main.cpp during run time.

Let's look inside main.cpp

#include <iostream>
#include "library.hpp"
int main ( void )
{
    printf("Calling the shared library\n");
    value_to_be_printed(10); /*calling value to be printed from shared library */
    printf("Completed\n");
    return 0;
}
        

Let's create shared library. You can file all these file in my git repo along with the neccessary step to create a shared library. (link to git)

You can run script.sh file inside the repo which will show you how to create a library.

No alt text provided for this image


Thanks and Happy Diwali to All In advance.

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics