What You'll Learn
- Header-only libraries
- Static libraries (.a / .lib)
- Shared/dynamic libraries (.so / .dll)
- Plugin architecture patterns
Creating & Using C++ Libraries
Libraries are how C++ code is shared and reused. Header-only libraries are the simplest — just #include and go. Static libraries are compiled once and embedded in your binary. Shared libraries are loaded at runtime, enabling plugins and reducing binary size.
Header-Only Libraries
Header-only libraries require no compilation or linking — just include the header. Templates and inline functions must be in headers anyway, making this the natural choice for generic code. Popular examples: nlohmann/json, Catch2, stb_image.
Pro Tip: Mark non-template functions as inline in header-only libraries to avoid multiple-definition linker errors when the header is included in multiple .cpp files.
Header-Only Library
Math and string utility libraries — just include and use
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// Header-only library — just #include and use
// No separate compilation, no linking step
// === math_utils.hpp ===
namespace mathlib {
template<typename T>
T clamp(T value, T low, T high) {
return max(low, min(value, high));
}
template<typename T>
T lerp(T a, T b, double t) {
return a + (b - a) * t;
}
inline double degToRad(double deg) { return deg *
...Static Libraries
A static library is an archive of compiled object files (.a on Linux/macOS, .lib on Windows). The linker copies the needed code into your executable. The result is self-contained — no runtime dependencies — but larger binaries.
Common Mistake: Forgetting -fPIC when building a static library that will be linked into a shared library. Position-Independent Code is required for shared library compatibility.
Static Library
Geometry library with build commands
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
// Static library (.a / .lib) — compiled once, linked into executable
// Build: g++ -c mylib.cpp -o mylib.o && ar rcs libmylib.a mylib.o
// Use: g++ main.cpp -L. -lmylib -o app
// === Simulated static library: geometry ===
namespace geometry {
const double PI = 3.14159265358979;
struct Circle {
double radius;
double area() const { return PI * radius * radius; }
doubl
...Plugin Architecture with Shared Libraries
Shared libraries (.so/.dll) enable plugin systems — load new functionality without recompiling the main program. Define a plugin interface, implement it in a shared library, and load it at runtime with dlopen/LoadLibrary.
Plugin System
Runtime-extensible plugin architecture
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <functional>
using namespace std;
// Plugin architecture — load functionality at runtime
// Real plugins use dlopen/LoadLibrary for .so/.dll files
// Plugin interface
class Plugin {
public:
virtual string name() const = 0;
virtual string version() const = 0;
virtual void execute(const string& input) = 0;
virtual ~Plugin() = default;
};
// Concrete plugins
class UppercasePlugin : public Plugin {
...Quick Reference
| Type | Extension | When to Use |
|---|---|---|
| Header-only | .hpp | Templates, small utilities |
| Static | .a / .lib | Self-contained binaries |
| Shared | .so / .dll | Plugins, shared across programs |
Lesson Complete!
You can now create header-only, static, and shared C++ libraries with plugin architectures.
Sign up for free to track which lessons you've completed and get learning reminders.