Creating Libraries (Static and Shared) in C++
In C++, libraries are an essential way to organize and reuse code across multiple programs. There are two main types of libraries: static libraries and shared libraries (also known as dynamic libraries). Static libraries are embedded directly into the executable at compile time, while shared libraries are linked at runtime. This article explains how to create both types of libraries in C++ with examples.
What is a Library?
A library in C++ is a collection of precompiled code that can be used by programs to avoid redundant coding. Libraries contain functions, classes, or objects that can be reused in various projects. They come in two types:
- Static Libraries: These are collections of object files that are linked directly into the executable at compile time. The library code becomes part of the final executable.
- Shared Libraries: These libraries are loaded at runtime and can be shared by multiple programs. They are not embedded in the executable but are linked dynamically when the program runs.
Creating a Static Library
Static libraries are created by compiling source code files into object files and then bundling them into a single archive file, typically with the .a
or .lib
extension.
Steps to Create a Static Library:
- Write the source code for the library.
- Compile the source code into object files.
- Create the static library archive file from the object files.
- Link the static library to your application.
Example of Creating a Static Library:
Let's create a simple static library that contains a function to add two numbers.
1. Write the source code for the library:
// math_functions.h #ifndef MATH_FUNCTIONS_H #define MATH_FUNCTIONS_H int add(int a, int b); #endif
// math_functions.cpp #include "math_functions.h" int add(int a, int b) { return a + b; }
2. Compile the source code into object files:
g++ -c math_functions.cpp -o math_functions.o
3. Create the static library archive:
ar rcs libmath.a math_functions.o
Now, you have created the static library libmath.a
that can be linked to any program.
4. Link the static library to your application:
g++ main.cpp -L. -lmath -o app
In this case, -L.
tells the compiler to look for libraries in the current directory, and -lmath
links the libmath.a
static library.
Creating a Shared Library
Shared libraries are dynamically linked at runtime. These libraries have the extension .so
on Linux and .dll
on Windows. Shared libraries allow multiple programs to share the same library in memory, reducing the overall memory footprint.
Steps to Create a Shared Library:
- Write the source code for the library.
- Compile the source code into position-independent code (PIC).
- Create the shared library from the object files.
- Link the shared library to your application at runtime.
Example of Creating a Shared Library:
Let's modify the previous example to create a shared library.
1. Write the source code for the library (same as before):
// math_functions.h #ifndef MATH_FUNCTIONS_H #define MATH_FUNCTIONS_H int add(int a, int b); #endif
// math_functions.cpp #include "math_functions.h" int add(int a, int b) { return a + b; }
2. Compile the source code into position-independent code:
g++ -fPIC -c math_functions.cpp -o math_functions.o
The -fPIC
flag is crucial when creating shared libraries because it generates position-independent code that can be loaded at any memory address at runtime.
3. Create the shared library:
g++ -shared -o libmath.so math_functions.o
This command creates the shared library libmath.so
.
4. Link the shared library to your application at runtime:
g++ main.cpp -L. -lmath -o app
Just like static libraries, you use -L
to specify the directory and -lmath
to link the shared library. However, shared libraries are linked dynamically at runtime.
Using Shared Libraries at Runtime
When using shared libraries, the program needs to be aware of the location of the shared library at runtime. You can set the library search path using the LD_LIBRARY_PATH
environment variable (on Linux) or the PATH
variable (on Windows).
Example of setting the LD_LIBRARY_PATH
:
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./app
Conclusion
Creating libraries in C++—whether static or shared—is an important technique for code reuse and modularity. Static libraries are easier to set up but increase the size of the executable, while shared libraries allow for more efficient memory usage at the cost of additional complexity in managing dependencies. By understanding the process of creating and linking both static and shared libraries, you can improve the structure and maintainability of your C++ projects.