原创

Qt中创建并运用动态链接库(共享库)的示例

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://shazhenyu.blog.csdn.net/article/details/52682450

1、先创建一个共享库的项目sharedlibrary。

sharedlibrary.pro:

 #-------------------------------------------------
 #
 # Project created by QtCreator shazhenyu
 #
 #-------------------------------------------------
 QT       += widgets
 TARGET = SharedLibrary
 TEMPLATE = lib
 DEFINES += SHAREDLIBRARY_LIBRARY
 SOURCES += sharedlibrary.cpp
 HEADERS += sharedlibrary.h\
        sharedlibrary_global.h
 unix {
    target.path = /usr/lib
    INSTALLS += target
 }

sharedlibrary_global.h:

 #ifndef SHAREDLIBRARY_GLOBAL_H
 #define SHAREDLIBRARY_GLOBAL_H
 #include <QtCore/qglobal.h>
 #if defined(SHAREDLIBRARY_LIBRARY)
 #  define SHAREDLIBRARYSHARED_EXPORT Q_DECL_EXPORT
 #else
 #  define SHAREDLIBRARYSHARED_EXPORT Q_DECL_IMPORT
 #endif
 #endif // SHAREDLIBRARY_GLOBAL_H

sharedlibrary.h:

 #ifndef SHAREDLIBRARY_H
 #define SHAREDLIBRARY_H
 #include "sharedlibrary_global.h"
 #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);  //自定义的外部函数
 }
 #endif // SHAREDLIBRARY_H

sharedlibrary.cpp:

 #include "sharedlibrary.h"
 #include <QPushButton>
 #include <QMessageBox>
 #include <QTime>
 #include <QPalette>
 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::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;
 }
 void SharedLibrary::onClicked()
 {
    QMessageBox::information(this, "Tip", "Shared Library...");
 } 
 SharedLibrary *getSharedLibrary()
 {
    return new SharedLibrary();
 }
 int add(int a, int b)
 {
    return a + b;
 }

2、创建测试共享库的项目TestSharedLibrary。

TestSharedLibrary.pro:

 #-------------------------------------------------
 #
 # Project created by QtCreator shazhenyu
 #
 #-------------------------------------------------
 QT       += core gui
 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 TARGET = TestSharedLibrary
 TEMPLATE = app
 INCLUDEPATH += ./SharedLibrary
 LIBS += -L$$PWD/SharedLibrary -lSharedLibrary
 SOURCES += main.cpp

main.cpp:

 #include <QApplication>
 #include "SharedLibrary/sharedlibrary.h"
 #include "SharedLibrary/sharedlibrary_global.h"
 #include <QLibrary>
 #include <QDebug>
 #include <QMessageBox>
 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);
    //声明所用到的dll文件
    QLibrary mylib("SharedLibrary/SharedLibrary.dll");
    //判断是否正确加载
    if (mylib.load())
    {
        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();
 }

文章最后发布于: 2016-09-27 17:24:26
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览