A small C++14 library for loading shared libraries at runtime on Linux and other POSIX systems.
pl::Plugin owns a dynamic loader handle. It opens a shared library, finds its
exported symbols, and closes the handle when the object is destroyed. Plugin
objects can be moved but cannot be copied.
- CMake 3.10 or newer
- C++14 compiler
- Make
- POSIX dynamic loader with
dlopenanddlsym
Create a separate build directory and configure a Release build.
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
makeThe build produces libc++plugins.so in the build directory.
From the build directory, run:
ctestThe integration test builds a small shared library, loads it with Plugin,
looks up an exported function, and verifies move construction and assignment.
Add this repository to your project and link the c++plugins target.
add_subdirectory(c++plugins)
target_link_libraries(my_application PRIVATE c++plugins)Include c++plugins/Plugin.h, then construct pl::Plugin with a shared
library path and a loader mode such as RTLD_NOW. Call load with an exported
symbol name. It returns the symbol address as void*.
#include <c++plugins/Plugin.h>
#include <iostream>
int main()
{
pl::Plugin plugin("./libexample-plugin.so", RTLD_NOW);
using NameFunction = const char *(*)();
const auto plugin_name =
reinterpret_cast<NameFunction>(plugin.load("plugin_name"));
std::cout << plugin_name() << '\n';
}The constructor and load throw std::invalid_argument when the dynamic
loader reports an error. The caller converts a successfully loaded address to
the correct function or object pointer type.
Licensed under the MIT License.