How to Create a Dynamic Library in C++?

In C++, dynamic libraries also known as shared libraries are a powerful way to modularize your code. They allow the users to build libraries that can be loaded at runtime by multiple applications at once. This approach promotes code reuse, reduces code duplication, and simplifies maintenance of the codebase. In this article, we will learn how to create and use a dynamic library in C++.

Creating Your Own Dynamic Library in C++

To create and use a dynamic library in C++ we can follow the below steps:

Step 1: Create the Header File.

First, we will create a header file that contains the declaration of the functions that are to be implemented. We can store this header file inside the new folder named include.

Folder Structure

Let’s name the header file as mymath.h:

C++
// mymath.h
// header guards
#pragma once
#ifndef MYMATH_H
#define MYMATH_H

// declaring functions
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
int divide(int a, int b);


#endif


We can add the implementation of the function here in the header file too but it is generally preferred to keep the implementation in the separate file.

Step 2: Create the Implementation File for the Header File

After declaring the header file, we must create an implementation file that will contain the actual code for the functions declared in the header file. This file will consist code for the function definitions which will be executed when the functions are called.

Let’s name this file mathlib.cpp:

C++
// including header
#include "mymath.h"

// addition implementation
int add(int a, int b) { return a + b; }

// substraction implementation
int subtract(int a, int b) { return a - b; }

// multiply implementation
int multiply(int a, int b) { return a * b; }

// divide implementation
int divide(int a, int b) { return a / b; }

Step 3: Compile the Dynamic Library using GCC

Now we will have to compile our source code into a dynamic library. One thing to note here is that the dynamic library extension in windows is (.dll) and in linux is (.so). So we need to name the files according to our system.

To compiler your source code into a dynamic library execute the following command in your terminal or cmd (make sure you have GCC installed):

g++ -fpic -shared {sourceFile} -I{headerFileDirectory} -o {libraryName.extension}

Here,

  • sourceFile: Source file name with relative location
  • headerFileDirectory: Relative directory to header file.
  • libraryName: Desired name of the library.
  • extension: Relevant extension (.so or .dll)

For Example, in this case

g++ -fpic -shared src/mymath.cpp -Iinclude -o mymath.dll

This will create the dynamic library in your specified directory.

Dynamic Library – mymath.dll is created in Windows

Note: The prefix “lib” should be added in the library name in Linux.

Step 4: Using the Dynamic Library

Now as we have created the dynamic library, we can use it in our applications. Let’s create a file named main.cpp and use the functions we have defined in our dynamic library.

C++
#include "mymath.h"
#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int b = 4;

    // calling functions defined inside header mymath.h
    cout << "Value of add(a, b): " << add(a, b) << endl;

    // calling function defined inside header mymath.h
    cout << "Value of multiply(a, b): " << multiply(a, b)
         << endl;

    return 0;
}


After that, execute the following command in your terminal/cmd to compile the main.cpp file:

g++ src/main.cpp -o main -Iinclude -lmymath -L../

Executable – main.exe is created in Windows

The executable file will be created for your program (.exe for windows and .out for Linux)

To execute the above code, use the following command in your terminal:

main      // for windows cmd
./main // for linux terminal

If you have trouble executing main in Linux, you may first need to tell the system about the dynamic library location. You can do it using the following:

"LD_LIBRARY_PATH="{relative path to .so file}" ./main

Output

Output of the Program using Custom Dynamic Library