Creating Dynamic Library in C++

To create a dynamic library in G++ compiler, follow the below steps:

1. Create Library Source Code and Header Files

Similar to the static library, we start by creating a header file with the .h extension and a source file with the .cpp extension.

Header File: myDynamicLibrary.h

C++
// myDynamicLibrary.h

#ifndef MYDYNAMICLIBRARY_H
#define MYDYNAMICLIBRARY_H

void sayHelloDynamic();
int multiplyNumbers(int a, int b);

#endif // MYDYNAMICLIBRARY_H

Source Code File: myDynamicLibrary.cpp

C++
// myDynamicLibrary.cpp 

#include "myDynamicLibrary.h" 
#include <iostream> 
using namespace std; 

// function 1 
void sayHelloDynamic() 
{ 
    cout << "Hello from the dynamic library!\n"; 
} 
// function 2 
int multiplyNumbers(int a, int b) { return a * b; }

2. Compile the Library Source Code to an Object File

Open a terminal in the directory containing myDynamicLibrary.cpp and run the following command:

g++ -fPIC -c myDynamicLibrary.cpp -o myDynamicLibrary.o

This command generates an object file (myDynamicLibrary.o) from the source code.

3. Create the Dynamic Library

Use the g++ compiler to create a dynamic library named libmyDynamicLibrary.so from the object file myDynamicLibrary.o. Run the following command:

g++ -shared -o libmyDynamicLibrary.so myDynamicLibrary.o

This command creates the dynamic library, and it is now ready to use in our program.

4. Write a Main Program that Uses the Dynamic Library

Create a new file named mainDynamic.cpp that uses the functions from the dynamic library.

mainDynamic.cpp:

C++
// mainDynamic.cpp

#include "myDynamicLibrary.h"
using namespace std;

int main()
{
    // calling sayHelloDynamic() function
    sayHelloDynamic();

    // calling multiplyNumbers function and storing the
    // result
    int result = multiplyNumbers(5, 7);
    cout << "The result is: " << result << "\n";

    return 0;
}

5. Compile the Main Program with Dynamic Library

Compile the mainDynamic.cpp file and link it with the dynamic library. For this, run the following command:

g++ mainDynamic.cpp -L. -lmyDynamicLibrary -o myDynamicProgram

This command links the mainDynamic.cpp file with your dynamic library (-lmyDynamicLibrary) and produces an executable named myDynamicProgram.

6. Run the Program

Run the compiled program.

./myDynamicProgram

Expected Output

Hello from the dynamic library!
The result is: 35




How Do I Create a Library in C++?

Libraries are reusable code packages that can be imported into our program to use the code defined in them. In C++, libraries can be either static or dynamic. A static library is a library that is linked to the program at compile-time whereas dynamic libraries in C++ are linked at runtime but they have the advantage that they don’t get included in the executable file, which keeps the executable size low. In this article, we will learn how to create a library in C++.

Similar Reads

Creating Static Library in C++

To create a static library in a G++ compiler, follow the below steps:...

Creating Dynamic Library in C++

To create a dynamic library in G++ compiler, follow the below steps:...