使用Qt创建动态和静态链接库
一、创建静态链接库点击打开链接
在Qt中创建静态库文件的主要步骤如下:静态链接库是将函数和数据编译成的一个二进制文件,Linux下的静态链接库是*.a文件,而在Windows下的静态链接库是*.LIB文件。编译器在连接的时候会恢复静态库文件中的函数和数据,并将它们和应用程序中的其它模块组合在一起生成可执行文件,因此,体积比较大。
1、新建一个创建C++库文件项目;2、构建项目,生成库文件;3、配置环境变量;
首先新建一个创建C++库文件项目,如下如所示:
项目源代码如下:
- //staticlibrary.h
- #ifndef STATICLIBRARY_H
- #define STATICLIBRARY_H
- #include <QDialog>
- class QPushButton;
- class StaticLibrary : public QDialog {
- Q_OBJECT
- public:
- StaticLibrary(QWidget *parent = 0);
- private:
- QPushButton *button;
- };
- #endif // STATICLIBRARY_H
- //staticlibrary.cpp
- #include "staticlibrary.h"
- #include <QtGui>
- StaticLibrary::StaticLibrary(QWidget *parent) : QDialog(parent)
- {
- setWindowTitle(tr("My Static Library"));
- button = new QPushButton(tr("staticLibrary"));
- QHBoxLayout *layout = new QHBoxLayout;
- layout->addWidget(button);
- setLayout(layout);
- }
这个文件即为静态链接库文件了。接下来使用这个静态链接库文件。
创建一个新的Qt GUI项目,源码如下
- //textlibrary.h
- #ifndef TEXTLIBRARY_H
- #define TEXTLIBRARY_H
- #include <QtGui/QDialog>
- class QPushButton;
- class TextLibrary : public QDialog
- {
- Q_OBJECT
- public:
- TextLibrary(QWidget *parent = 0);
- ~TextLibrary();
- private:
- QPushButton *button;
- private slots:
- void on_button_clicked();
- };
- #endif // TEXTLIBRARY_H
- //textlibrary.cpp
- #include "textlibrary.h"
- #include "mylib.h"
- #include "staticlibrary.h"
- #include <QtGui>
- TextLibrary::TextLibrary(QWidget *parent)
- : QDialog(parent)
- {
- button = new QPushButton(tr("test"));
- setWindowTitle(tr("Test Library"));
- QHBoxLayout *layout = new QHBoxLayout;
- layout->addWidget(button);
- setLayout(layout);
- connect(button, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
- }
- TextLibrary::~TextLibrary()
- {
- }
- void TextLibrary::on_button_clicked()
- {
- StaticLibrary w;
- w.show();
- w.exec();
- }
- //main.cpp
- #include <QtGui/QApplication>
- #include "textlibrary.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- TextLibrary w;
- w.show();
- return a.exec();
- }
现在就要将前面创建的库文件使用到这个项目中了,在.pro文件中添加.a文件的路径,以及头文件的路径。
- LIBS += F:\library\staticLibrary\debug\libstaticLibrary.a
- INCLUDEPATH += F:\library\staticLibrary
其中右图为左图按钮test的效果。
二、创建动态链接库文件
动态链接库文件是在运行的时候被应用程序使用。在使用动态链接库的时候,一般需要提供两个文件:
接下来使用Qt创建动态链接库文件,步骤与创建静态库文件类似。源代码如下:1.引入库文件(.lib):引入库文件与静态链接库文件的后缀名相同,但是并不相同。引入库文件主要包含了DLL文件导出的函数和变量的符号名。2.DLL(.dll):包含实际的函数和数据。在使用动态链接库的情况下,在编译链接可执行文件时,只需要引入库文件即可,但是在运行可执行程序时,则需要加载相应的DLL文件。
- //mylib.h
- #ifndef MYLIB_H
- #define MYLIB_H
- #include "myLib_global.h"
- #include <QDialog>
- class QPushButton;
- class MYLIBSHARED_EXPORT MyLib : public QDialog {
- public:
- MyLib(QWidget *parent = 0);
- private:
- QPushButton *button;
- };
- #endif // MYLIB_H
- //mylib.cpp
- #include "mylib.h"
- #include <QtGui>
- MyLib::MyLib(QWidget *parent) : QDialog(parent)
- {
- setWindowTitle(tr("My Lib"));
- button = new QPushButton(tr("mylib"));
- QHBoxLayout *layout = new QHBoxLayout;
- layout->addWidget(button);
- setLayout(layout);
- }
然后配置环境变量,在.pro中添加:
- LIBS += F:\library\myLib\debug\myLib.dll #这是编译期的链接库路径
- INCLUDEPATH += F:\library\myLib
在运行的时候,你可能得不到任何的结果。这是因为,虽然我们在LIBS中添加了dll文件的路径,但是这里的添加路径编译器时的查找路径,在运行时并不是从这里查找,而是先从当前项目目录中先找,然后从系统变量path的路径中去找。因此,有两种方法可以解决这个问题,将dll文件拷贝到当前目录中,或者在系统变量path中添加dll的路径,然后重启电脑。然后运行结果如下:
前面说到,动态链接库包括两个文件,引入库文件和DLL文件,为什么这里只需要将DLL文件拷贝到当前目录下呢?这是因为引入库文件中包含的是DLL文件的函数和变量的符号名,用来供给应用程序的编译通过,而在.pro文件中配置LIBS的路径即为编译阶段的查找路径,因此这里的引入库文件就不需要拷贝即可在编译阶段被编译器查找得到。