How to create shared library (.SO) in C++ (G++)?

Get FREE domain for 1st year and build your brand new site

Binary Tree Problems books

To create a shared library in C++ using G++, compile the C++ library code using GCC/ G++ to object file and convert the object file to shared (.SO) file using gcc/ g++. The code can be used during the compilation of code and running executable by linking the .SO file using G++.

// Convert library code to Object file
g++ -c -o library.o library.c

// Create shared .SO library
gcc -shared -o libfoo.so library.o

To use it with a client code using the library, use the following commands:

# Create the executable by linking shared library
gcc -L<path to .SO file> -Wall -o code main.c -l<library name>

# Make shared library available at runtime
export LD_LIBRARY_PATH=<path to .SO file>:$LD_LIBRARY_PATH

# Run executable
./a.out

Shared libraries contain external library code which can be used by multiple client systems. This is memory efficient as only one copy is maintained and is used by multiple programs across the system.

In constrast to archive library, to run client code on a different system, the shared library .SO file needs to be transfered to the new system.

There are four steps:

  • Compile C++ library code to object file (using g++)

  • Create shared library file (.SO) using gcc --shared

  • Compile the C++ code using the header library file using the shared library (using g++)

  • Set LD_LIBRARY_PATH

  • Run the executable (using a.out)

  • Step 1: Compile C code to object file

gcc -c -o library.o library.c

There are two options:

  • c: to specify the creation of object file

  • o: to specify the name of the final object file

  • Step 2: Create shared library file using object file

gcc -shared -o libfoo.so library.o

There are two options:

  • shared: to specify the creation of shared library

  • o: to specify the name of the resulting library file

  • Step 3: Compile C++ code

gcc -Llib/ -Wall -o code main.c -llibrary
  • Step 4: Set LD_LIBRARY_PATH
export LD_LIBRARY_PATH=lib/:$LD_LIBRARY_PATH
  • Step 5: Run the archive code
./code

This involves four major files:

  • library.hpp: Library header file
  • library.cpp: Library C++ file
  • library.o: Object file of library.cpp
  • library.so: shared library of the above library
  • code.cpp: C++ code using the library through header file
  • a.out: executable

Example

In this example, we will create a C++ library and use it in a C++ code. We will build our library as an shared library and link it to our code to generate an executable independent of library.

Our library will have a simple function print_value() which will take in an Integer and print it to the console.

Header file "library.hpp":

#ifndef LIB1_H_INCLUDED
#define LIB1_H_INCLUDED

#ifdef __cplusplus
   extern ā€œCā€ {
#endif

void print_value ( int x );

#ifdef __cplusplus
   }
#endif

#endif /* LIB1_H_INCLUDED */

Let us create the code for the above library header file in the code file "library.cpp":

#include "library.hpp"
#include <stdio.h>

void print_value( int x )
{
    cout << x << "\n";
}

Save the code as library.cpp. Compile the above code as:

g++ -std=c++14 -c -o library.o library.cpp

Create the archive file library.a using the object file:

gcc -shared -o liblibrary.so library.o

Using the library

Use the library like this in a code file named "main.cpp":

#include <stdio.h>
#include "library.hpp"

int main ( void )
{
    print_value(10);
    return 0;
}

Create the executable:

g++ -Llib/ -Wall -o code main.cpp -llibrary

This will create the executable a.out which will run on any compatible machine with the library files (.SO). To help the executable find the shared library, set the LD_LIBRARY_PATH to the path of the .SO file.

export LD_LIBRARY_PATH=lib/:$LD_LIBRARY_PATH

Run the executable:

./code

With this, we have created a shared file and generated an executable using it. Enjoy.