libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
loadLib.cpp
// Example to load symbols from a dynamic library.
// This executable loads a DLL/SO file whose name can be be unknown until run-time.
// See the dynamicLib.cpp example for how to define symbols in a library.
#include <iostream>
#include <z/file/execdir.hpp>
#include <z/file/library.hpp>
int main() {
// Assume library is in same dir as executable, not necessarily the working dir.
// Note that you do not have to specify the file extension of the library;
// by default, the correct extension will be appended based on your OS.
auto libName = z::file::execdir() + "/dynamicLib";
if (!lib.load(libName)) {
std::cout << "Unable to load lib!" << std::endl;
return -1;
}
// Load a symbol, assuming it to be a function that returns void and has no parameters.
auto func = lib.function<void()>("test");
if (!func) {
std::cout << "Unable to load symbol \"test\"." << std::endl;
return -1;
}
std::cout << "Loaded symbol \"test\" as function. Result:" << std::endl;
func();
// It's a good habit to unload the library when we no longer need it.
lib.unload();
return 0;
}
A class for loading dynamic libraries.
Definition library.hpp:16
bool unload() noexcept
Unload the dynamic library.
bool load(const zpath &fileName, bool autoExtension=true) noexcept
Load a dynamic library with the given file name.
std::function< T > function(const zpath &symbolName) noexcept
Get a pointer to the function with the given name.
Definition library.hpp:111
zpath execdir() noexcept
Get the directory path of the running executable.
Definition execdir.hpp:11