How to Create a Dynamic Library in C++?
Last Updated :
07 Jun, 2024
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 StructureLet'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 WindowsNote: 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 WindowsThe 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
Similar Reads
How Do I Create a Library in C?
Libraries are a collection of code that can be used by other programs. They promote code reuse and modularity. In C, we can create our own libraries. In this article, we will learn how to create a library in C. Creating a Library in CIn C, there are multiple methods using which we can create a libra
3 min read
How To Create Custom Arduino Library Using C++
What is the Arduino library? Libraries are a collection of precompiled, reusable code or routines which are used by developers to reduce the development time. Arduino libraries are written in C or C++. These libraries provide us with a convenient way to share code. Arduino IDE already consists of a
6 min read
Rust - Creating a Library
Rust is a multi-paradigm programming language like C++ syntax that was designed for performance and safety, especially for safe concurrency. Also, it is a compiled system programming language. In this article, we will see how to create libraries in Rust. Creating a Rust Library:Step 1: We start by c
1 min read
Strings in C++ and How to Create them?
Strings in C++ are used to store text or sequences of characters. In C++ strings can be stored in one of the two following ways: C-style string (using characters)String class Each of the above methods is discussed below: 1. C style string: In C, strings are defined as an array of characters. The dif
2 min read
Creating and Using DLL (Class Library) in C#
A class library file is a collection of classes and namespaces in C# without any entry point method like Main. Once we create a class library file it can be used in the C# project and classes inside it can be used as required. Class Library makes it convenient to use functionalities by importing DLL
4 min read
Dynamic _Cast in C++
In C++, dynamic_cast is a cast operator that converts data from one type to another type at runtime. It is mainly used in inherited class hierarchies for safely casting the base class pointer or reference to derived class (called downcasting). To work with dynamic_cast, there must be one virtual fun
5 min read
How to add "graphics.h" C/C++ library to gcc compiler in Linux
While trying c graphic programming on Ubuntu, I figured out that graphic.h is not a standard C library and it is not supported by gcc compiler. So I am writing this article to explain the process.If you want to use graphics.h on Ubuntu platform you need to compile and install libgraph. It is the imp
2 min read
How to Install a C# Class Library in Visual Studio?
A C# library project is a separate project used to hold utility classes. So this might be the class that handles our database or might handle some communications with the network. In our case, we are going to create a math library that is a stand-in for some of those other cases. It is a single sour
3 min read
Reference to Dynamic Objects in C++
In C++, the objects can be created at run-time. C++ supports two operators new and delete to perform memory allocation and de-allocation. These types of objects are called dynamic objects. The new operator is used to create objects dynamically and the delete operator is used to delete objects dynami
3 min read
File System Library in C++17
In this article, we will learn about the File System library in C++17 and with examples. <filesystem> header was added in C++17 and introduces a set of classes, functions, and types that simplify file system operations. In simple words, we can say that the filesystem library provides tools tha
6 min read