Skip to content

About

A small C++14 library for loading shared libraries at runtime on Linux and other POSIX systems.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

Repository files navigation

c++plugins

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.

Requirements

  • CMake 3.10 or newer
  • C++14 compiler
  • Make
  • POSIX dynamic loader with dlopen and dlsym

Build

Create a separate build directory and configure a Release build.

mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make

The build produces libc++plugins.so in the build directory.

Test

From the build directory, run:

ctest

The integration test builds a small shared library, loads it with Plugin, looks up an exported function, and verifies move construction and assignment.

Use from CMake

Add this repository to your project and link the c++plugins target.

add_subdirectory(c++plugins)
target_link_libraries(my_application PRIVATE c++plugins)

API

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.

License

Licensed under the MIT License.

About

A small C++14 library for loading shared libraries at runtime on Linux and other POSIX systems.

Resources

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages