http://doc.qt.nokia.com/latest/sharedlibrary.html
The following sections list certain things that should be taken into account when creating shared libraries.
Using Symbols from Shared Libraries
Symbols - functions, variables or classes - contained in shared libraries intended to be used by clients, such as applications or other libraries, must be marked in a special way. These symbols are called public symbols that areexported or made publicly visible.
The remaining symbols should not be visible from the outside. On most platforms, compilers will hide them by default. On some platforms, a special compiler option is required to hide these symbols.
When compiling a shared library, it must be marked forexport. To use the shared library from a client, some platforms may require a special import declaration as well.
Depending on your target platform, Qt provides special macros that contain the necessary definitions:
- Q_DECL_EXPORT must be added to the declarations of symbols used when compiling a shared library.
- Q_DECL_IMPORT must be added to the declarations of symbols used when compiling a client that uses the shared library.
Now, we need to ensure that the right macro is invoked -- whether we compile a share library itself, or just the client using the shared library. Typically, this can be solved by adding a special header.
Let us assume we want to create a shared library calledmysharedlib. A special header for this library,mysharedlib_global.h, looks like this:
#include <QtCore/QtGlobal> #if defined(MYSHAREDLIB_LIBRARY) # define MYSHAREDLIB_EXPORT Q_DECL_EXPORT #else # define MYSHAREDLIB_EXPORT Q_DECL_IMPORT #endif
In the .pro file of the shared library, we add:
DEFINES += MYSHAREDLIB_LIBRARY
In each header of the library, we specify the following:
#include "mysharedlib_global.h" MYSHAREDLIB_EXPORT void foo(); class MYSHAREDLIB_EXPORT MyClass...
This ensures that the right macro is seen by both library and clients. We also use this technique in Qt's sources.
Header File Considerations
Typically, clients will include only the public header files of shared libraries. These libraries might be installed in a different location, when deployed. Therefore, it is important to exclude other internal header files that were used when building the shared library.
For example, the library might provide a class that wraps a hardware device and contains a handle to that device, provided by some 3rd-party library:
#include <footronics/device.h> class MyDevice { private: FOOTRONICS_DEVICE_HANDLE handle; };
A similar situation arises with forms created by Qt Designer when using aggregation or multiple inheritance:
#include "ui_widget.h" class MyWidget : public QWidget { private: Ui::MyWidget m_ui; };
When deploying the library, there should be no dependency to the internal headers footronics/device.h or ui_widget.h.
This can be avoided by making use of the Pointer to implementation idiom described in various C++ programming books. For classes with value semantics, consider using QSharedDataPointer.
Binary compatibility
For clients loading a shared library, to work correctly, the memory layout of the classes being used must match exactly the memory layout of the library version that was used to compile the client. In other words, the library found by the client at runtime must be binary compatible with the version used at compile time.
This is usually not a problem if the client is a self-contained software package that ships all the libraries it needs.
However, if the client application relies on a shared library that belongs to a different installation package or to the operating system, then we need to think of a versioning scheme for shared libraries and decide at which level Binary compatibility is to be maintained. For example, Qt libraries of the same major version number are guaranteed to be binary compatible.
Maintaining Binary compatibility places some restrictions on the changes you can make to the classes. A good explanation can be found at KDE - Policies/Binary Compatibility Issues With C++. These issues should be considered right from the start of library design. We recommend that the principle of Information hiding and thePointer to implementation technique be used wherever possible.
相关推荐
创建Qt动态链接库的步骤主要包括以下几个部分: 1. **项目配置**:在Qt Creator中新建项目时,选择“库”类别,并在类型选项中选择“动态库”。配置编译器和构建套件,确保它们与目标平台兼容。 2. **头文件和源...
3. **链接库**:对于静态库,Qt Creator会自动链接;对于动态库,需要在“链接器”设置中添加相应的.lib文件。 4. **运行时依赖**:如果是动态库,还需要确保运行环境有对应.dll文件,可以将其拷贝到可执行文件同一...
本教程将详细介绍如何在Qt5环境下创建和使用动态链接库。动态链接库(.dll文件)允许我们把功能模块化,降低代码的耦合度,同时减少应用程序的体积,因为多个程序可以共享同一个库。 首先,我们需要创建一个动态...
在这个“Linux qtcreator动态加载动态链接库案例”中,我们将深入探讨如何在Qt Creator中实现动态加载动态链接库(.so文件)。动态链接库允许我们在运行时加载代码,而不是在编译时,这有助于减少应用程序的大小和...
QT动态链接库是Qt框架的核心组成部分,它允许开发者创建跨平台的应用程序,支持Windows、Linux、macOS等多种操作系统。在编程领域,动态链接库(Dynamic Link Library,DLL)是一种共享库,程序在运行时才加载这些库...
本文将深入探讨Qt动态链接库的创建、使用以及如何在Qt项目中调用C++编译的外部库。 首先,让我们了解动态链接库(DLL)。DLL是一种Windows操作系统中的共享库,它包含可由多个程序同时使用的代码和数据。动态链接...
实现功能:利用VS2012封装生成动态链接库(.dell和.lib)给QT调用 文件包含:1.VS2012生成动态库工程 2.QT5.5.1调用VS2012动态库工程(注释内含调用具体方法) 3.QT调用运行结果图片
本篇文章将深入探讨如何在Linux下的Qt环境中创建和使用动态库。 首先,创建动态库涉及以下步骤: 1. **项目设置**:在Qt Creator中,新建一个Qt Console Application项目。选择“New File or Project” -> ...
**完美编译mupdf-qt动态链接库详解** mupdf-qt是MuPDF的Qt界面版本,它提供了一个轻量级、高效且可定制的PDF查看器,支持多种平台。在Windows环境下,通常使用Visual Studio(VS)作为编译工具,配合Qt库来构建...
创建QT动态链接库的步骤如下: 1. **设置项目属性**:在QT Creator中,创建一个新的项目,并选择“库”->“动态库”作为项目类型。在项目配置中,确保选择了适当的编译器和目标平台。 2. **编写源代码**:定义你要...
### QT中动态链接库的建立与使用详解 #### 动态链接库(DLL)概念解析 动态链接库(Dynamic Link Library,简称DLL)是Windows操作系统中的一种共享库,它允许多个应用程序共享相同的代码和数据。DLL文件包含了可以由...
通过以上步骤,我们可以在Eclipse中成功地开发和管理使用Qt动态链接库的项目。了解如何有效地管理这些库是提高开发效率和确保应用程序稳定性的关键。同时,不断学习和掌握Qt的新特性以及Eclipse的高级功能,将进一步...
Linux Qt 动态链接库和静态链接库学习笔记 Linux Qt 是一个基于Qt框架的应用程序开发平台,而动态链接库和静态链接库是Linux平台上常用的库链接方式。了解动态链接库和静态链接库的使用是 Qt 开发者必备的技能。 ...
使用C++ Builder或Qt Creator编写一个简单的C++动态链接库,该库暴露一个函数,用于执行HMAC-SHA计算。确保库的接口设计为标准C,以便Delphi可以轻松地调用。 2. 在Delphi中建立C++库的包装: 创建一个新的Delphi...
创建QT动态链接库的第一步是设置工程配置。在QT Creator中,你需要创建一个新的项目,并选择“库”类别下的“动态库”模板。接着,定义你的头文件和源文件,比如`Calculator.h`和`Calculator.cpp`,在其中实现一个...
动态库,也称为动态链接库(DLL),在Windows系统中是可执行代码的共享库,而在其他系统如Linux中,通常被称为共享对象(SO)。动态库在程序运行时被加载,而不是在编译时,这使得多个应用程序可以共享同一份库的...
本文主要讨论在Qt Creator环境下,如何在Mac操作系统中正确地调用动态链接库。在Windows系统中,动态链接库(DLL)通常以.lib和.dll文件形式存在,而在Mac OS中,对应的动态库则是以.a静态库和.dylib动态库的形式...
首先,我们需要理解Qt动态链接库的创建过程: 1. **创建动态链接库项目**:在Qt Creator中,选择“新建项目” -> “库” -> “动态库”,然后配置项目的编译器和目标平台。在.pro文件中,将QMAKE_EXT_DLL设置为.lib...
动态库的制作包括两种方式,c++的类方式和C语言的函数方式,本文都有详细说明 qtcreator调用动态库dll,本文写了两种方法 各个工程都提供下载 我的制作环境:qt5.15.2 MinGW编译器 windows10系统
标题中的“可以在Winxp上运行的Qt5.5动态库”指的是这个压缩包包含的是针对Windows XP系统优化的Qt5.5版本的动态链接库(DLL)。Qt是一个跨平台的应用程序开发框架,广泛用于创建图形用户界面和其他软件。动态链接库...