#include <Pt/System/Library.h>
Shared library loader. More...
Public Member Functions | |
Library () | |
Default Constructor which does not load a library. | |
Library (const Path &path) | |
Loads a shared library. More... | |
Library (const Library &other) | |
Copy constructor. | |
~Library () | |
The destructor unloads the shared library from memory. | |
void | close () |
Closes the shared library. | |
Symbol | getSymbol (const char *symbol) const |
Resolves the symbol symbol from the shared library. More... | |
Library & | open (const Path &path) |
Loads a shared library. More... | |
operator const void * () const | |
Returns null if invalid. | |
bool | operator! () const |
Returns true if invalid. | |
Library & | operator= (const Library &other) |
Assignment operator. | |
void * | operator[] (const char *symbol) const |
Resolves the symbol symbol from the shared library Returns the address of the symbol or 0 if it was not found. | |
const Path & | path () const |
Returns the path to the shared library image. | |
void * | resolve (const char *symbol) const |
Resolves the symbol symbol from the shared library Returns the address of the symbol or 0 if it was not found. | |
Static Public Member Functions | |
static const char * | prefix () |
Returns the prefix for shared libraries. More... | |
static const char * | suffix () |
Returns the extension for shared libraries. More... | |
The Pt::System::Library class can be used to dynamically load shared libraries and resolve symbols from it. It also provides the static functions prefix() and suffix(), which allow to build library names in a portable way. The next example shows how to load a library with the basename "MyLib" at runtime and how to retrieve the address of the function "myFunction":
The constructor of the Library class will try to load the library at the given path. If no library could be found, the path is extended by the platform-specific library extension first, and then also by the shared library prefix. If no library could be found at either path, an AccessFailed exception is thrown. The function getSymbol() returns a Symbol object when the library symbol could be resolved or a SymbolNotFound exception, if the symbol name could not be found. The actual address of the library symbol is returned by sym(). Alternatively, the index operator can be used to load symbols, which will return the address of the symbol as a pointer to void or a nullptr on failure. Note, that standard C++ does not allow to cast void pointers to function pointers, but nearly all runtimes implement that as an extension.
If a file could not be found at the given path, the path will be extended by the platform-specific shared library extension first and then also by the shared library prefix. If still no file can be found an exception of type AccessFailed is thrown. Otherwise, the library is loaded immediately.
If a file could not be found at the given path, the path will be extended by the platform-specific shared library extension first and then also by the shared library prefix. If still no file can be found an exception of type AccessFailed is thrown. Otherwise, the library is loaded immediately. Calling this method twice might close the previously loaded library.
Symbol getSymbol | ( | const char * | symbol | ) | const |
Throws SymbolNotFound if the symbol could not be resolved.
|
static |
Returns ".so" on Linux, ".dll" on Windows.
|
static |
Returns "lib" on Linux, "" on Windows