QLibrary Class
头文件: | #include <QLibrary> |
qmake: | QT += core |
基类: | QObject |
Note: All functions in this class are reentrant.
公有类型
enum | LoadHint { ResolveAllSymbolsHint, ExportExternalSymbolsHint, LoadArchiveMemberHint, PreventUnloadHint, DeepBindHint } |
flags | LoadHints |
属性
- 1 个属性继承自 QObject
公有函数
QLibrary(QObject *parent = Q_NULLPTR) | |
QLibrary(const QString &fileName, QObject *parent = Q_NULLPTR) | |
QLibrary(const QString &fileName, int verNum, QObject *parent = Q_NULLPTR) | |
QLibrary(const QString &fileName, const QString &version, QObject *parent = Q_NULLPTR) | |
~QLibrary() | |
QString | errorString() const |
QString | fileName() const |
bool | isLoaded() const |
bool | load() |
LoadHints | loadHints() const |
QFunctionPointer | resolve(const char *symbol) |
void | setFileName(const QString &fileName) |
void | setFileNameAndVersion(const QString &fileName, int versionNumber) |
void | setFileNameAndVersion(const QString &fileName, const QString &version) |
void | setLoadHints(LoadHints hints) |
bool | unload() |
- 32 个公有函数继承自 QObject
静态公有成员
bool | isLibrary(const QString &fileName) |
QFunctionPointer | resolve(const QString &fileName, const char *symbol) |
QFunctionPointer | resolve(const QString &fileName, int verNum, const char *symbol) |
QFunctionPointer | resolve(const QString &fileName, const QString &version, const char *symbol) |
- 11 个静态公有成员继承自 QObject
其他继承的成员
详细描述
QLibrary 用于运行时加载库.
QLibrary 的实例用于操作一个动态链接库文件 (这里我们称为 "library", 也可以称为 "DLL"). QLibrary 提供访问库中函数的一种平台无关方式. 你可以在构造时传递库文件名, 也可以通过 setFileName() 给对象显式设置. 加载库时, 除非文件名是绝对路径, QLibrary 在所有系统指定的位置搜索 (如. Unix上的 LD_LIBRARY_PATH
).
如果文件路径是绝对路径, 则会首先尝试在这个位置加载. 如果找不到, QLibrary 尝试不同系统相关的前后缀的文件名, 比如 Unix 的前缀 "lib", 后缀 ".so" Mac 的后缀 ".dylib" Windows 的后缀 ".dll".
如果文件路径不是绝对路径, QLibrary 改变搜索顺序, 首先尝试系统特定的前后缀, 之后是特定文件路径.
这让使用除去前后缀的库基本名称来指定库文件变得可能. 因此代码可以在不同操作系统里执行, 但不用太多代码尝试各种文件名称.
最重要的函数是 load(), 动态加载库, isLoaded() 检查是否加载成功, resolve() 解析库中的符号. 如果库还没加载, resolve() 函数隐式地加载这个库. 多个 QLibrary 实例访问同一个物理库文件是可行的. 一旦被加载, 库在内存中一直保留到程序结束. 你可以调用 unload() 尝试卸载一个库,但如果有其他 QLibrary 实例在使用同一个库文件, 调用会失败, 只有在每一个实例都调用过 unload()后, 库才会真正卸载.
A typical use QLibrary 的一种典型用法是解析库中的导出符号, 并调用其对应的C语言函数, 这叫做显式链接, 对应于隐式链接. 隐式链接是构建中的链接可执行文件和静态库的步骤.
下面的代码片段加载了个库, 解析 "mysymbol"符号, 并在一切就绪的情况下调用这个函数. 如果出现了问题, 如. 文件不存在或者符号未定义, 函数指针将会是 0, 且不会调用.
QLibrary myLib("mylib"); typedef void (*MyPrototype)(); MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol"); if (myFunction) myFunction();
符号必须作为C函数导出, resolve() 才能工作. 这意味着用 C++ 编译器编译的函数必须由 extern "C"
块包裹. 在Windows上, 还要求导出函数要使用 dllexport
宏; 详见 resolve(). 方便起见, resolve() 函数有静态形式, 你可以在不现实加载库的情况下使用:
typedef void (*MyPrototype)(); MyPrototype myFunction = (MyPrototype) QLibrary::resolve("mylib", "mysymbol"); if (myFunction) myFunction();
参见 QPluginLoader.
成员类型
enum QLibrary::LoadHint
flags QLibrary::LoadHints
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.
Constant | Value | Description |
---|---|---|
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 | 0x04 | Allows 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. |
QLibrary::PreventUnloadHint | 0x08 | Prevents the library from being unloaded from the address space if close() is called. The library's static variables are not reinitialized if open() is called at a later time. |
QLibrary::DeepBindHint | 0x10 | Instructs the linker to prefer definitions in the loaded library over exported definitions in the loading application when resolving external symbols in the loaded library. This option is only supported on Linux. |
The LoadHints type is a typedef for QFlags<LoadHint>. It stores an OR combination of LoadHint values.
参见 loadHints.
属性
fileName : QString
This property holds the file name of the library
We recommend omitting the file's suffix in the file name, since QLibrary will automatically look for the file with the appropriate suffix (see isLibrary()).
When loading the library, QLibrary searches in all system-specific library locations (for example, 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".
访问函数:
QString | fileName() const |
void | setFileName(const QString &fileName) |
loadHints : LoadHints
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 loadHints 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.
Note: Setting this property after the library has been loaded has no effect and loadHints() will not reflect those changes.
Note: This property is shared among all QLibrary instances that refer to the same library.
访问函数:
LoadHints | loadHints() const |
void | setLoadHints(LoadHints hints) |
成员函数
QLibrary::QLibrary(QObject *parent = Q_NULLPTR)
Constructs a library with the given parent.
QLibrary::QLibrary(const QString &fileName, QObject *parent = Q_NULLPTR)
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 macOS and iOS, and ".dll" on Windows. (See fileName.)
QLibrary::QLibrary(const QString &fileName, int verNum, QObject *parent = Q_NULLPTR)
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 macOS and iOS, and ".dll" on Windows. (See fileName.)
QLibrary::QLibrary(const QString &fileName, const QString &version, QObject *parent = Q_NULLPTR)
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 macOS and iOS, and ".dll" on Windows. (See fileName.)
QLibrary::~QLibrary()
Destroys the QLibrary object.
Unless unload() was called explicitly, the library stays in memory until the application terminates.
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.
This function was introduced in Qt 4.2.
[static]
bool QLibrary::isLibrary(const QString &fileName)
Returns true
if fileName has a valid suffix for a loadable library; otherwise returns false
.
Platform | Valid suffixes |
---|---|
Windows | .dll , .DLL |
Unix/Linux | .so |
AIX | .a |
HP-UX | .sl , .so (HP-UXi) |
macOS and iOS | .dylib , .bundle , .so |
Trailing versioning numbers on Unix are ignored.
bool QLibrary::isLoaded() const
Returns true
if the library is loaded; otherwise returns false
.
参见 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.
参见 unload().
QFunctionPointer QLibrary::resolve(const char *symbol)
Returns the address of the exported symbol symbol. The library is loaded if necessary. The function returns 0 if the symbol could not be resolved or if the library could not be loaded.
Example:
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
[static]
QFunctionPointer QLibrary::resolve(const QString &fileName, const char *symbol)
This is an overloaded function.
Loads the library fileName and returns the address of the exported symbol symbol. Note that fileName should not include the platform-specific file suffix; (see fileName). The library remains loaded until the application exits.
The function returns 0 if the symbol could not be resolved or if the library could not be loaded.
参见 resolve().
[static]
QFunctionPointer QLibrary::resolve(const QString &fileName, int verNum, const char *symbol)
This is an overloaded function.
Loads the library fileName with major version number verNum and returns the address of the exported symbol symbol. Note that fileName should not include the platform-specific file suffix; (see fileName). The library remains loaded until the application exits. verNum is ignored on Windows.
The function returns 0 if the symbol could not be resolved or if the library could not be loaded.
参见 resolve().
[static]
QFunctionPointer QLibrary::resolve(const QString &fileName, const QString &version, const char *symbol)
This is an overloaded function.
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; (see fileName). The library remains loaded until the application exits. version is ignored on Windows.
The function returns 0 if the symbol could not be resolved or if the library could not be loaded.
This function was introduced in Qt 4.4.
参见 resolve().
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.
参见 setFileName().
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.
This function was introduced in Qt 4.4.
参见 setFileName().
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 shouldn't 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().
Note that on Mac OS X 10.3 (Panther), dynamic libraries cannot be unloaded.