![]() |
VOOZH | about |
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++.
To create a static library in a G++ compiler, follow the below steps:
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
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
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.
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.
Now, we can use the created static library in another C++ program.
main.cpp:
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_executableThis command links the main.cpp file with your static library (-lmath_operations ) and produces an executable named main_executable.
Run the compiled program using following command:
./main_executableExpected Output
Addition: 15
Subtraction: 5
To create a dynamic library in G++ compiler, follow the below steps:
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
Source Code File: myDynamicLibrary.cpp
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.
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.oThis command creates the dynamic library, and it is now ready to use in our program.
Create a new file named mainDynamic.cpp that uses the functions from the dynamic library.
mainDynamic.cpp:
Compile the mainDynamic.cpp file and link it with the dynamic library. For this, run the following command:
g++ mainDynamic.cpp -L. -lmyDynamicLibrary -o myDynamicProgramThis command links the mainDynamic.cpp file with your dynamic library (-lmyDynamicLibrary) and produces an executable named myDynamicProgram.
Run the compiled program.
./myDynamicProgramExpected Output
Hello from the dynamic library!
The result is: 35