CopperSpice API  1.7.2
QLibrary Class Reference

The QLibrary class loads shared libraries at runtime. More...

Inheritance diagram for QLibrary:
QObject

Public Typedefs

using LoadHints = QFlags< LoadHint >
 

Public Types

enum  LoadHint
 

Public Methods

 QLibrary (const QString &fileName, const QString &version, QObject *parent=nullptr)
 
 QLibrary (const QString &fileName, int verNum, QObject *parent=nullptr)
 
 QLibrary (const QString &fileName, QObject *parent=nullptr)
 
 QLibrary (QObject *parent=nullptr)
 
 ~QLibrary ()
 
QString errorString () const
 
QString fileName () const
 
bool isLoaded () const
 
bool load ()
 
LoadHints loadHints () const
 
void * resolve (const QString &symbol)
 
void setFileName (const QString &fileName)
 
void setFileNameAndVersion (const QString &fileName, const QString &version)
 
void setFileNameAndVersion (const QString &fileName, int versionNumber)
 
void setLoadHints (LoadHints hints)
 
bool unload ()
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &objName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Static Public Methods

static bool isLibrary (const QString &fileName)
 
static void * resolve (const QString &fileName, const QString &symbol)
 
static void * resolve (const QString &fileName, const QString &version, const QString &symbol)
 
static void * resolve (const QString &fileName, int verNum, const QString &symbol)
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 

Properties

 fileName
 
 loadHints
 
- Properties inherited from QObject
 objectName
 

Additional Inherited Members

- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 
- Public Slots inherited from QObject
void deleteLater ()
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 

Detailed Description

The QLibrary class loads shared libraries at runtime.

An instance of a QLibrary object operates on a single shared object file (which we call a "library", but is also known as a "DLL"). A QLibrary provides access to the functionality in the library in a platform independent way. You can either pass a file name in the constructor, or set it explicitly with setFileName(). When loading the library, QLibrary searches in all the system-specific library locations (e.g. LD_LIBRARY_PATH on Unix), unless the file name has an absolute path. If the file can not be found, QLibrary tries the name with different platform-specific file suffixes, like ".so" on Unix, ".dylib" on the Mac, or ".dll" on Windows. This makes it possible to specify shared libraries that are only identified by their basename (i.e. without their suffix), so the same code will work on different operating systems.

The most important functions are load() to dynamically load the library file, isLoaded() to check whether loading was successful, and resolve() to resolve a symbol in the library. The resolve() function implicitly tries to load the library if it has not been loaded yet. Multiple instances of QLibrary can be used to access the same physical library. Once loaded, libraries remain in memory until the application terminates. You can attempt to unload a library using unload(), but if other instances of QLibrary are using the same library, the call will fail, and unloading will only happen when every instance has called unload().

A typical use of QLibrary is to resolve an exported symbol in a library, and to call the C function that this symbol represents. This is called "explicit linking" in contrast to "implicit linking", which is done by the link step in the build process when linking an executable against a library.

The following code snippet loads a library, resolves the symbol "mysymbol", and calls the function if everything succeeded. If something goes wrong, e.g. the library file does not exist or the symbol is not defined, the function pointer will be 0 and will not be called.

QLibrary myLib("mylib");
typedef void (*MyPrototype)();
MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol");
if (myFunction) {
myFunction();
}

The symbol must be exported as a C function from the library for resolve() to work. This means that the function must be wrapped in an extern "C" block if the library is compiled with a C++ compiler. On Windows, this also requires the use of a dllexport macro. Refer to resolve() for the details of how this is done. For convenience, there is a static resolve() function which you can use if you just want to call a function in a library without explicitly loading the library first:

typedef void (*MyPrototype)();
MyPrototype myFunction = (MyPrototype) QLibrary::resolve("mylib", "mysymbol");
if (myFunction) {
myFunction();
}
See also
QPluginLoader

Member Typedef Documentation

Typedef for QFlags<LoadHint>. Refer to QLibrary::LoadHint for documentation.

Member Enumeration Documentation

This enum describes the possible hints that can be used to change the way libraries are handled when they are loaded. These values indicate how symbols are resolved when libraries are loaded, and are specified using the setLoadHints() function.

ConstantValueDescription
QLibrary::ResolveAllSymbolsHint 0x01 Causes all symbols in a library to be resolved when it is loaded, not simply when resolve() is called.
QLibrary::ExportExternalSymbolsHint 0x02 Exports unresolved and external symbols in the library so that they can be resolved in other dynamically-loaded libraries loaded later.
QLibrary::LoadArchiveMemberHint 0x04Allows the file name of the library to specify a particular object file within an archive file. If this hint is given, the filename of the library consists of a path, which is a reference to an archive file, followed by a reference to the archive member.
See also
loadHints

Constructor & Destructor Documentation

QLibrary::QLibrary ( QObject parent = nullptr)
explicit

Constructs a library with the given parent.

QLibrary::QLibrary ( const QString fileName,
QObject parent = nullptr 
)
explicit

Constructs a library object with the given parent that will load the library specified by fileName.

We recommend omitting the file's suffix in fileName, since QLibrary will automatically look for the file with the appropriate suffix in accordance with the platform, e.g. ".so" on Unix, ".dylib" on Mac OS X, and ".dll" on Windows.

See also
fileName
QLibrary::QLibrary ( const QString fileName,
int  verNum,
QObject parent = nullptr 
)
explicit

Constructs a library object with the given parent that will load the library specified by fileName and major version number verNum. Currently, the version number is ignored on Windows.

We recommend omitting the file's suffix in fileName, since QLibrary will automatically look for the file with the appropriate suffix in accordance with the platform, e.g. ".so" on Unix, ".dylib" on Mac OS X, and ".dll" on Windows.

See also
fileName
QLibrary::QLibrary ( const QString fileName,
const QString version,
QObject parent = nullptr 
)
explicit

Constructs a library object with the given parent that will load the library specified by fileName and full version number version. Currently, the version number is ignored on Windows.

We recommend omitting the file's suffix in fileName, since QLibrary will automatically look for the file with the appropriate suffix in accordance with the platform, e.g. ".so" on Unix, ".dylib" on Mac OS X, and ".dll" on Windows.

See also
fileName
QLibrary::~QLibrary ( )

Destroys the QLibrary object.

Unless unload() was called explicitly, the library stays in memory until the application terminates.

See also
isLoaded(), unload()

Method Documentation

QString QLibrary::errorString ( ) const

Returns a text string with the description of the last error that occurred. Currently, errorString will only be set if load(), unload() or resolve() for some reason fails.

QString QLibrary::fileName ( ) const

Returns the value of the property fileName.

bool QLibrary::isLibrary ( const QString fileName)
static

Returns true if fileName has a valid suffix for a loadable library, otherwise returns false.

PlatformValid suffixes
Windows.dll, .DLL
Unix/Linux.so
AIX.a
HP-UX.sl, .so (HP-UXi)
Mac OS X.dylib, .bundle, .so

Trailing versioning numbers on Unix are ignored.

bool QLibrary::isLoaded ( ) const

Returns true if the library is loaded, otherwise returns false.

See also
load()
bool QLibrary::load ( )

Loads the library and returns true if the library was loaded successfully, otherwise returns false. Since resolve() always calls this function before resolving any symbols it is not necessary to call it explicitly. In some situations you might want the library loaded in advance, in which case you would use this function.

See also
unload()
LoadHints QLibrary::loadHints ( ) const

Returns the value of the loadHints property.

void * QLibrary::resolve ( const QString fileName,
const QString symbol 
)
static

Loads the library fileName and returns the address of the exported symbol symbol. The fileName> should not include the platform specific file suffix. The library remains loaded until the application exits.

Returns a nullptr if the symbol could not be resolved or if the library could not be loaded.

See also
resolve(), fileName
void * QLibrary::resolve ( const QString fileName,
const QString version,
const QString symbol 
)
static

Loads the library fileName with full version number version and returns the address of the exported symbol symbol. Note that fileName should not include the platform-specific file suffix. The library remains loaded until the application exits. version is ignored on Windows.

Returns a nullptr if the symbol could not be resolved or if the library could not be loaded.

See also
resolve(), fileName
void * QLibrary::resolve ( const QString fileName,
int  verNum,
const QString symbol 
)
static

Loads the library fileName with major version number verNum and returns the address of the exported symbol symbol. The fileName should not include the platform specific file suffix. The library remains loaded until the application exits. The value for verNum is ignored on Windows.

Returns a nullptr if the symbol could not be resolved or if the library could not be loaded.

See also
resolve(), fileName
void * QLibrary::resolve ( const QString symbol)

Returns the address of the exported symbol symbol. The library is loaded if necessary. Returns a nullptr if the symbol could not be resolved or if the library could not be loaded.

typedef int (*AvgFunction)(int, int);
AvgFunction avg = (AvgFunction) library->resolve("avg");
if (avg) {
return avg(5, 8);
} else {
return -1;
}

The symbol must be exported as a C function from the library. This means that the function must be wrapped in an extern "C" if the library is compiled with a C++ compiler. On Windows you must also explicitly export the function from the DLL using the __declspec(dllexport) compiler directive, for example:

extern "C" MY_EXPORT int avg(int a, int b) {
return (a + b) / 2;
}

with MY_EXPORT defined as

#ifdef Q_OS_WIN
#define MY_EXPORT __declspec(dllexport)
#else
#define MY_EXPORT
#endif
void QLibrary::setFileName ( const QString fileName)

Sets the value of the property fileName to fileName.

void QLibrary::setFileNameAndVersion ( const QString fileName,
const QString version 
)

Sets the fileName property and full version number to fileName and version respectively. The version parameter is ignored on Windows.

See also
setFileName()
void QLibrary::setFileNameAndVersion ( const QString fileName,
int  versionNumber 
)

Sets the fileName property and major version number to fileName and versionNumber respectively. The versionNumber is ignored on Windows.

See also
setFileName()
void QLibrary::setLoadHints ( LoadHints  hints)

Sets the value of the property loadHints to hints.

bool QLibrary::unload ( )

Unloads the library and returns true if the library could be unloaded, otherwise returns false. This happens automatically on application termination, so you should not normally need to call this function. If other instances of QLibrary are using the same library, the call will fail, and unloading will only happen when every instance has called unload().

See also
resolve(), load()

Property Documentation

QLibrary::fileName

This property holds the file name of the library. It is recommended to omit the file's suffix in the file name since QLibrary will automatically look for the file with the appropriate suffix.

When loading the library, QLibrary searches in all system-specific library locations (e.g. LD_LIBRARY_PATH on Unix), unless the file name has an absolute path. After loading the library successfully, fileName() returns the fully-qualified file name of the library, including the full path to the library if one was given in the constructor or passed to setFileName().

For example, after successfully loading the "GL" library on Unix platforms, fileName() will return "libGL.so". If the file name was originally passed as "/usr/lib/libGL", fileName() will return "/usr/lib/libGL.so".

See also
isLibrary()
PropertiesClass Methods
read fileName
write setFileName
QLibrary::loadHints

This property holds give the load() function some hints on how it should behave.

You can give some hints on how the symbols are resolved. Usually, the symbols are not resolved at load time, but resolved lazily, (that is, when resolve() is called). If you set the loadHint to ResolveAllSymbolsHint, then all symbols will be resolved at load time if the platform supports it.

Setting ExportExternalSymbolsHint will make the external symbols in the library available for resolution in subsequent loaded libraries.

If LoadArchiveMemberHint is set, the file name is composed of two components: A path which is a reference to an archive file followed by the second component which is the reference to the archive member. For instance, the fileName libGL.a(shr_64.o) will refer to the library shr_64.o in the archive file named libGL.a. This is only supported on the AIX platform.

The interpretation of the load hints is platform dependent, and if you use it you are probably making some assumptions on which platform you are compiling for, so use them only if you understand the consequences of them.

By default, none of these flags are set, so libraries will be loaded with lazy symbol resolution, and will not export external symbols for resolution in other dynamically-loaded libraries.

PropertiesClass Methods
read loadHints
write setLoadHints