C++ cross-platform wrapper around dynamic loading of shared libraries (dll, so, dylib)
MIT License
CPack
installationPublished by martin-olivier about 1 year ago
MinGW
support, requested by @lonnietcCPack
support, requested by @KOLANICHWIN32_LEAN_AND_MEAN
macro redefinition, thanks to @Aleksoid1978CMake
improvements, thanks to @rmaxi-meworkflows
improvementsPublished by martin-olivier over 2 years ago
std::filesystem::path
support in dylib contructordylib::add_filename_decorations
and dylib::no_filename_decorations
constantsdylib::get_symbol
public member functiondylib::native_symbol_type
public member type definitionPublished by martin-olivier over 2 years ago
open()
, close()
and default constructor
are not available anymore
// old API
dylib lib("./libs/libfoo", dylib::extension);
// new API
dylib lib("./libs", "foo");
The dylib class can now load a dynamic library from the system library path
// Load "foo" library from the system library path
dylib lib("foo");
The dylib class will now automatically add os decorations (os prefix and suffix) to the library name, but you can disable that by setting decorations parameter to false
// 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", false);
The return type of get_function
is now a function pointer instead of an std::function
// old API
std::function<double(double, double)> adder = lib.get_function<double(double, double)>("adder");
// new API
double (*adder)(double, double) = lib.get_function<double(double, double)>("adder");
handle_error
exception is now called load_error
try {
dylib lib("foo");
lib.get_function<double(double, double)>("adder");
}
// old API
catch (const dylib::handle_error &e) {
std::cout << e.what() << std::endl;
}
// new API
catch (const dylib::load_error &e) {
std::cout << e.what() << std::endl;
}
To ensure the functions and variable of your future dynamic library will be exported on windows, you can use the following macro
#if defined(_WIN32) || defined(_WIN64)
#define LIB_EXPORT __declspec(dllexport)
#else
#define LIB_EXPORT
#endif
Or, you can add the following cmake rule that will export all symbols on windows
if(MSVC)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif()
Make sure to mark the functions and variables you want to export as
extern "C"
to avoid name mangling
Since dylib
now handles libs prefix, you will need to remove the following cmake rule when building your dynamic libs
# remove the following rule from your cmakelists if you was using it with dylib 1.X.X
set_target_properties(target PROPERTIES PREFIX "")
Huge thanks to @eyalroz for his help and contributions that made this update possible !
Published by martin-olivier over 2 years ago
CMake
build system, thanks to @plevoldcpplint
Published by martin-olivier over 2 years ago
native_handle
functionfetchContent
examplestd::string
overload for ctor
and open
Published by martin-olivier over 2 years ago
nullptr
is passed as argument in get_function
or get_variable
when no dynamic lib is currently loadedPublished by martin-olivier over 2 years ago
has_symbol
functionoperator bool()
void dylib_symbol_example(const dylib &lib)
{
if (lib.has_symbol("GetModule"))
std::cout << "GetModule has been found" << std::endl;
else
std::cout << "Could not found GetModule symbol" << std::endl;
}
void dylib_status_example(const dylib &lib)
{
if (lib)
std::cout << "something is curently loaded in the dylib object" << std::endl;
if (!lib)
std::cout << "nothing is curently loaded in the dylib object" << std::endl;
}
Published by martin-olivier almost 3 years ago
CMakeLists.txt
Published by martin-olivier almost 3 years ago
DYLIB_API
macro to be able to export symbolssnake_case
win32
check on CMakeLists instead of a basic else
Published by martin-olivier almost 3 years ago
WIN32_LEAN_AND_MEAN
flag for windowsPublished by martin-olivier about 3 years ago
GetLastError
and FormatMessage
Published by martin-olivier about 3 years ago
T &
to be able to alter the value inside the dylibdlerror
on unix and generic error on windowsPublished by martin-olivier about 3 years ago
this
before open
and close
functionsclose
function, m_handle
will be assigned to nullptr only if it wasn't already nullptrPublished by martin-olivier over 3 years ago
constexpr
Published by martin-olivier over 3 years ago
Published by martin-olivier over 3 years ago
std::move
on DyLib::exception
Published by martin-olivier over 3 years ago
&& ctor
and operator=(&&)
overloadsctor
and open
overloadsDyLib::extension
Published by martin-olivier over 3 years ago
DyLib
now detects the OS file extensiondefined(_WIN32)
or defined(_WIN64)
noexcept
keyword when it was possiblePublished by martin-olivier over 3 years ago
nullptr
as argumentm_openLib
for windows is now static