DynamicLibLoader v0.9.7
dynamic_library.hpp is a single-file header-only library for loading dynamic libraries across platforms.
Highlights
- Single-File Header-Only: Just include
dynamic_library.hpp— no other dependencies. - Optimized & Modern C++: Refactored for clarity, safety, and efficiency. Uses RAII, move semantics, type traits, mutexes, and unordered maps. Symbol caching (
invoke()) minimizes repeated lookups. - Cross-Platform: Windows (.dll), Linux (.so), macOS (.dylib)
- RAII Resource Management: Automatically loads/unloads dynamic libraries.
- Error Handling: Throws
std::runtime_errorwith detailed platform-specific error messages. - Cached & Uncached Symbol Access:
invoke()caches symbols for efficiency;invoke_uncached()avoids caching.
Usage Example
#include "dynamic_library.hpp"
dll::dynamic_library lib("mylib.dll"); // or .so / .dylib
auto func = lib.get<int(int,int)>("my_function");
int result = func(1, 2);
// Cached invocation
int cached_result = lib.invoke<int(int,int)>("my_function", 3, 4);DynamicLibLoader v0.9.7
dynamic_library.hpp 是一个 单文件 Header-Only 库,用于跨平台加载动态库。
主要特点
- 单文件 Header-Only: 只需包含
dynamic_library.hpp,无需任何额外依赖。 - 优化 & 现代 C++: 使用 C++11+ 写法重构,提升可读性、性能与安全性。使用 RAII、移动语义、类型特性、互斥锁和无序映射。符号缓存 (
invoke()) 减少重复查找,提高调用效率。 - 跨平台支持: Windows (.dll)、Linux (.so)、macOS (.dylib)
- RAII 资源管理: 自动加载/卸载动态库,确保资源安全释放。
- 错误处理: 抛出
std::runtime_error并附带平台相关详细错误信息。 - 符号访问:
invoke()会缓存符号以提升效率;invoke_uncached()不缓存符号。
使用示例
#include "dynamic_library.hpp"
dll::dynamic_library lib("mylib.dll"); // 或 .so / .dylib
auto func = lib.get<int(int,int)>("my_function");
int result = func(1, 2);
// 使用缓存调用
int cached_result = lib.invoke<int(int,int)>("my_function", 3, 4);