Open In App

How Do I Create a Library in C++?

Last Updated : 18 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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++.

Creating Static Library in C++

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

1. Create Header and Library Source Code Files

First, open your preferred text editor or IDE and start off by creating a new header file with the .h a and declaring the functions that we want to have in the library.

Header File name: math_operations.h

C++
// math_operations.h
#ifndef MATH_OPERATIONS_H
#define MATH_OPERATIONS_H

int add(int a, int b);
int subtract(int a, int b);

#endif

Now, create another file that is the source code file with .cpp extension and write the function body of the function that we declared in the header file.

Source Code File: math_operations.cpp

C++
// math_operations.cpp
#include "math_operations.h"

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

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

2. Compiling the Library Source Code to Object File

Compile the source code into object files. By opening the terminal in the directory containing the source code file (math_operations.cpp) and run the following command:

g++ -c math_operations.cpp -o math_operations.o

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

3. Create the Static Library

Use the ar (archive) command to bundle the object files into a static library and create a static library named libmath_operations.a from the object file math_operations.o. Run the following command:

ar rcs libmath_operations.a math_operations.o

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

4. Write a Main Program that Uses the Static Library

Now, we can use the created static library in another C++ program.

main.cpp:

C++
// main.cpp
#include "math_operations.h"
#include <iostream>
using namespace std;

int main()
{
    int a = 10, b = 5;

    cout << "Addition: " << add(a, b) << endl;
    cout << "Subtraction: " << subtract(a, b) << endl;

    return 0;
}

5. Compile the Main Program Using the Static Library

Run the following command to compile the main.cpp file and link it with the static library:

g++ main.cpp -L. -lmath_operations -o main_executable

This command links the main.cpp file with your static library (-lmath_operations ) and produces an executable named main_executable.

6. Execute the Program

Run the compiled program using following command:

./main_executable

Expected Output

Addition: 15
Subtraction: 5

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




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads