一:动态库
1.创建
SharedLibrary.pro
QT += widgets
TARGET = SharedLibrary
TEMPLATE = lib
DEFINES += SHAREDLIBRARY_LIBRARY
SOURCES += sharedlibrary.cpp
HEADERS += sharedlibrary.h\
sharedlibrary.h
#include <QtCore/qglobal.h>
#if defined(SHAREDLIBRARY_LIBRARY)
# define SHAREDLIBRARYSHARED_EXPORT Q_DECL_EXPORT
#else
# define SHAREDLIBRARYSHARED_EXPORT Q_DECL_IMPORT
#endif
#include <QWidget>
class SHAREDLIBRARYSHARED_EXPORT SharedLibrary : public QWidget
{
Q_OBJECT
public:
explicit SharedLibrary(QWidget *parent = 0);
void updateBackground();
int subtract(int a, int b);
private slots:
void onClicked();
};
extern "C" {
SHAREDLIBRARYSHARED_EXPORT SharedLibrary* getSharedLibrary(); //获取类SharedLibrary对象
SHAREDLIBRARYSHARED_EXPORT int add(int a, int b); //自定义的外部函数
}
sharedlibrary.cpp
#include "sharedlibrary.h"
#include <QPushButton>
#include <QMessageBox>
#include <QTime>
SharedLibrary::SharedLibrary(QWidget *parent)
: QWidget(parent)
{
QPushButton *pButton = new QPushButton(this);
pButton->setText("Test Shared Library");
connect(pButton, SIGNAL(clicked()), this, SLOT(onClicked()));
}
void SharedLibrary::onClicked()
{
QMessageBox::information(this, "Tip", "Shared Library...");
}
void SharedLibrary::updateBackground()
{
QTime time;
time = QTime::currentTime();
qsrand(time.msec() + time.second()*1000);
int nR = qrand() % 256;
int nG = qrand() % 256;
int nB = qrand() % 256;
QPalette palette(this->palette());
palette.setColor(QPalette::Background, QColor(nR, nG, nB));
this->setPalette(palette);
}
int SharedLibrary::subtract(int a, int b)
{
return a - b;
}
SharedLibrary* getSharedLibrary()
{
return new SharedLibrary();
}
int add(int a, int b)
{
return a + b;
}
2.使用
SharedLibraryTest.pro
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = SharedLibraryApplication
TEMPLATE = app
SOURCES += main.cpp
HEADERS +=
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/SharedLibrary/x86/release/ -lSharedLibrary
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/SharedLibrary/x86/debug/ -lSharedLibrary
else:unix: LIBS += -L$$PWD/SharedLibrary/x86/ -lSharedLibrary
INCLUDEPATH += $$PWD/SharedLibrary/x86/Debug
DEPENDPATH += $$PWD/SharedLibrary/x86/Debug
main.cpp
#include <QApplication>
#include "SharedLibrary/sharedlibrary.h"
#include <QDebug>
#include <QMessageBox>
#include <QLibrary>
//#include <>
typedef SharedLibrary* (*GetSharedLibrary)();//定义函数指针,获取类TestDLL对象;
typedef int (*Fun)(int, int); //定义函数指针,int add(int a,int b);
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SharedLibrary w;
w.resize(400, 200);
w.updateBackground();
w.show();
int nSubtract = w.subtract(10, 4);
int nAdd = add(5, 5);
QMessageBox::information(NULL, "Tip", QString("subtract:%1 add:%2").arg(nSubtract).arg(nAdd));
// //声明所用到的dll文件
// QLibrary mylib("D:/WorkSpace/Qt Projects/SharedLibraryApplication/SharedLibrary/SharedLibrary.dll");
// qDebug() << "mylib path: " << mylib.fileName();
// //判断是否正确加载
// if (mylib.load())
// {
// qDebug() << "library loaded";
// GetSharedLibrary getSharedLibrary = (GetSharedLibrary)mylib.resolve("getSharedLibrary");
// Fun add = (Fun)mylib.resolve("add");
// if (getSharedLibrary)
// {
// SharedLibrary *pLib = getSharedLibrary();
// pLib->updateBackground();
// pLib->subtract(10, 4);
// pLib->show();
// pLib->setWindowTitle("Test ShareLibrary");
// }
// else
// {
// qDebug() << "Could not show widget from the loaded library";
// }
// if (add)
// {
// int nResult = add(5, 5);
// QMessageBox::information(NULL, "Tip", QString::number(nResult));
// }
// else
// {
// qDebug() << "Could not add from the loaded library";
// }
// }
// //加载失败
// else
// {
// qDebug() << mylib.errorString();
// }
return a.exec();
}
注意点:Qt生成的动态库不能提供给VS使用。