How to create a library in C++
Before going on to the creation of a library, let’s understand its meaning.
What is a library?
In simple words, a library is a collection of numerous functions, methods, classes, header files etc. One or more header files and an object library make up a C++ library. The class definitions and other information required to access library functions are provided in the header files. Your software is linked with the object library's-built functions and data to create an executable program.
The C++ class libraries are reusable, modular pieces of code. You can integrate sections of code that have already been developed and tested using class libraries.
The most famous library in C++ is Standard Template Library, which means STL, that almost contains every possible thing, which is very helpful in implementing Data Structures and Algorithms.
Segments of the C++ Standard Library can be characterized as follows:
The Standard Function Library: The Standard Function Library is a collection of all-purpose, independent functions that are not contained in any specific class. The function library comes from C by inheritance.
The Object-Oriented Class Library: The object-oriented class library is a collection of classes and related functions.
The point to be noted is that libraries are also classified as Static and Dynamic based on their type.
Static Library: Procedures in a static library are compiled and linked into your program directly. When you generate a program that uses a static library, the executable results include every feature that the static library offers. Static libraries have the benefit of requiring only the executable to be distributed for users to run your software. This guarantees that the appropriate version of the library is always used with your software because the library becomes a part of it. Additionally, you can use static libraries much like the functionality you've created for your own application because they become a part of it. But the disadvantage of a static library is it wastes a lot of memory and can't be upgraded easily. Also, in order to upgrade them, again, all the files that are executable have to be replaced.
Dynamic Library: When running, a program will load procedures from a dynamic library (also called a shared library). A dynamic library does not merge with your executable when you generate a program that requires it; instead, it stays as a different thing. Therefore, extension Dynamic libraries provide the benefit of allowing multiple programs to share a single copy, which saves storage space. The ability to upgrade the dynamic library without having to change all the executables that depend on it may be of more significant benefit.
Since dynamic libraries are not linked to your program, programs that use dynamic libraries must manually load and communicate with the dynamic library. This method makes interacting with a dynamic library challenging and confusing.
The DLLs can be loaded dynamically during run time. Usually, programmers have to write code to do so, like LoadLibrary or LoadLibraryEx. You will need to include the right header files before you can use anything from a library. The compiler knows the prototypes of functions and can check for the correct spelling of function names and for the valid number and type of parameters you pass to it in calls. The library files tell the compiler how to call the function in the DLL.
Note: Both static and dynamic libraries will have different extensions depending upon the usage of Operating Systems like Windows, Linux etc.
Advantages
A. Usually, libraries come in the form of meaning 2, a collection of code that is compiled separately. Sharing common stuff at the source code level can be done, but it might not be the best practice.
B. Those that are essential and can be seen as an extension of the language. This is how you can deal with, for instance, files in C and C++. Also, the entire STL is a great example. Nowadays. Things like vectors and lists are considered essential.
C. Third-party or open-source libraries that are written by others, maybe for profit.
D. Libraries wrote within your company, mainly or exclusively for use within their own projects.
E. Standard code, so other developers understand what it does straight away. Tested code in lots of apps. Many bugs have been flushed out, and many person-hours of development work will be saved.
Now let’s see the working of a library:
Program to demonstrate the working of a Library:
#include <iostream>
#include<stack>
using namespace std;
int main()
{
stack<int>st;
st.push(4);
st.push(3);
st.push(2);
st.push(1);
st.pop();
while(!st.empty())
{
int x=st.top();
cout<<x<<endl;
st.pop();
}
return 0;
}
Output:
2
3
4
Explanation: Here, in the header file, we included a library called #include<stack>. This particular library contains all inbuilt methods, functions, etc., related to stack. In the program, we can see the operations like push, pop, top, empty etc., and as they are already implemented and stored in the stack library, there is absolutely no need for us to implement them again; we can directly use them.
Now, let's see the creation of a static library.
Creation of a static library
1.Create a C++ file that has any methods, functions in the library.
Let the file name be Filename: lib_mylibrary.cpp
#include <iostream>
using namespace std;
void func(void)
{
cout<<"func() is a functon which is called from a static library"<<endl;
}
2. Create a header file with .h extension for the library
Filename is : lib_mylibrary.h
void func(void);
3. Compile library files.
g++ -c lib_mylibrary.cpp -o lib_mylibrary.o
4. Static library creation. In this stage, many object files will be combined into a single static library.
ar rcs lib_mylibrary.a lib_mylibrary.o
5. Our static library is now usable. At this point, all we have to do to use lib mylib.a is copy it somewhere else. Let's keep the library in the current directory for demonstration purposes.
Working:
1. Create a cpp file with main function
#include "lib_mylibrary.h"
void main()
{
func();
}
2. Compile the driver program.
g++ -c driver.cpp -o driver.o
3. Now, it’s time to link the static library & compiled driver program to. Here, -L defines the location of a static library.
g++ -o driver driver.o -L. -l_mylibrary
4. Run the driver program
func() is a functon, which is called from a static library
Let’s see the creation of a dynamic library.
1.Create a C++ file that has any methods, functions in the library.
Let the file name be Filename: lib_mylibrary.cpp
#include "lib_mylibrary.h"
void main()
{
func();
}
2. Create a header file with .h extension for the library
Filename is : lib_mylibrary.h
void func(void);
3. Compile library files.
g++ -shared -o lib_mylibrary.so lib_mylibrary.o
The “-shared” flag instructs to generate a shared library.
4. Static library creation. In this stage, many object files will be combined into a single static library.
ar rcs lib_mylibrary.a lib_mylibrary.o
5. Our static library is now usable. At this point, all we have to do to use lib mylib.a is copy it somewhere else. Let's keep the library in the current directory for demonstration purposes.
Working:
1. Create a cpp file with main function
#include "lib_mylibrary.h"
void main()
{
func();
}
2. Compile the driver or main program.
g++ -c driver.o -o driver.so -o a.out
3. Now, it’s time to link the static library & compiled driver program to. Here, -L defines the location of a static library.
g++ -o driver driver.o -L. -l_mylibrary a.out
4. Run the driver program
func() is a functon which is called from a dynamic library.
So far, we have seen the creation of static and dynamic libraries as well as their working and implementation. Let’s have a quick overview of it.
Overview
Libraries come in two types:
Static libraries and dynamic (shared) libraries.
At the time of compilation, static libraries are copied into the executable.
Dynamic libraries are loaded and linked at runtime rather than being duplicated.
How to use static and dynamic libraries in Linux.
LD LIBRARY PATH and -rpath is the two ways to provide a library search path for dynamic libraries.
A library that contains several functions and possibly other items like #defines, constants, classes, etc., that you may use in your application by including the header files. You must instruct your compiler where to find the library's source code, for example, by including the files in your project in your IDE. You may now use the library's functionality without having to create it yourself by reading the documentation, help, and comments. It is frequently detailed how to use the functions, what they accomplish, what parameters are required, and what is returned, maybe with coding examples.
A DLL or shared library has already been independently compiled, similar to what was described before (.so). You only need to have enough of the library's source code to link to it now, not the whole thing. As a result, you'll also require lib files, DLL files, and .so files. Libraries from third parties may or may not come with their complete source code. They are partly protected against others stealing their work by only providing the headers, dll, and library files rather than the source code. Programs can also load more quickly by choosing to load some DLLs later when they are required and take up less memory because other processes can share DLLs. Then you only need one copy of the DLL in memory.
DLL stands for Dynamic Link Library. This word can be used to refer to libraries more generally or specifically to Windows.
Libraries in Unix or Linux have the .so file extension, which is also used as a shorthand to refer to libraries on these operating systems. You must create a separate project and specify to your IDE that the project type is a library rather than an executable or a different kind in order to create a library in meaning two on your own as well.