Coder Social home page Coder Social logo

vcarpani / dylib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from martin-olivier/dylib

0.0 0.0 0.0 232 KB

C++ cross-platform wrapper around dynamic loading of shared libraries (dll, so, dylib)

Home Page: https://conan.io/center/recipes/dylib

License: MIT License

C++ 73.26% CMake 26.74%

dylib's Introduction

dylib

version license cpp

ci coverage

The goal of this C++ library is to load dynamic libraries (.so, .dll, .dylib) and access its functions and global variables at runtime.

โญ Don't forget to put a star if you like the project!

Compatibility

Works on Linux, MacOS and Windows

Installation

Using a package manager

You can install dylib from vcpkg or conan center:

vcpkg install dylib
conan install --requires=dylib/2.2.1

Using CMake Fetch

You can also fetch dylib to your project using CMake:

include(FetchContent)

FetchContent_Declare(
    dylib
    GIT_REPOSITORY "https://github.com/martin-olivier/dylib"
    GIT_TAG        "v2.2.1"
)

FetchContent_MakeAvailable(dylib)

Documentation

Constructor

The dylib class can load a dynamic library from the system library path

// Load "foo" library from the system library path

dylib lib("foo");

The dylib class can also load a dynamic library from a specific path

// Load "foo" library from relative path "./libs"

dylib lib("./libs", "foo");

// Load "foo" library from full path "/usr/lib"

dylib lib("/usr/lib", "foo");

The dylib class will automatically add the filename decorations of the current os to the library name, but you can disable that by setting decorations parameter to dylib::no_filename_decorations

// Windows -> "foo.dll"
// MacOS   -> "libfoo.dylib"
// Linux   -> "libfoo.so"

dylib lib("foo");

// Windows -> "foo.lib"
// MacOS   -> "foo.lib"
// Linux   -> "foo.lib"

dylib lib("foo.lib", dylib::no_filename_decorations);

Get a function or a variable

get_function
Get a function from the dynamic library currently loaded in the object

get_variable
Get a global variable from the dynamic library currently loaded in the object

// Load "foo" dynamic library

dylib lib("foo");

// Get the function "adder" (get_function<T> will return T*)

auto adder = lib.get_function<double(double, double)>("adder");

// Get the variable "pi_value" (get_variable<T> will return T&)

double pi = lib.get_variable<double>("pi_value");

// Use the function "adder" with "pi_value"

double result = adder(pi, pi);

Miscellaneous tools

has_symbol
Returns true if the symbol passed as parameter exists in the dynamic library, false otherwise

get_symbol
Get a symbol from the dynamic library currently loaded in the object

native_handle
Returns the dynamic library handle

dylib lib("foo");

if (lib.has_symbol("GetModule") == false)
    std::cerr << "symbol 'GetModule' not found in 'foo' lib" << std::endl;

dylib::native_handle_type handle = lib.native_handle();
dylib::native_symbol_type symbol = lib.get_symbol("GetModule");

assert(handle != nullptr && symbol != nullptr);
assert(symbol == dlsym(handle, "GetModule"));

Exceptions

load_error
This exception is raised when the library failed to load or the library encountered symbol resolution issues

symbol_error
This exception is raised when the library failed to load a symbol

Those exceptions inherit from dylib::exception

try {
    dylib lib("foo");
    double pi_value = lib.get_variable<double>("pi_value");
    std::cout << pi_value << std::endl;
} catch (const dylib::load_error &) {
    std::cerr << "failed to load 'foo' library" << std::endl;
} catch (const dylib::symbol_error &) {
    std::cerr << "failed to get 'pi_value' symbol" << std::endl;
}

Example

A full example about the usage of the dylib library is available HERE

Tests

To build unit tests, enter the following commands:

cmake . -B build -DDYLIB_BUILD_TESTS=ON
cmake --build build

To run unit tests, enter the following command inside build directory:

ctest

Community

If you have any question about the usage of the library, do not hesitate to open a discussion

If you want to report a bug or provide a feature, do not hesitate to open an issue or submit a pull request

Contributing

Set the cmake flag DYLIB_BUILD_TESTS to ON to enable tests and make it easier for you to contribute

cmake . -B build -DDYLIB_BUILD_TESTS=ON

Do not forget to sign your commits and use conventional commits when providing a pull request

git commit -s -m "feat: ..."

dylib's People

Contributors

martin-olivier avatar breigner01 avatar aleksoid1978 avatar dqhntt avatar jeffersongt avatar kolanich avatar rmaxi-me avatar plevold avatar dolphywind avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.