qt使用動態庫(DLL)

本文主要講解在QT開發環境中如何使用VC生成的DLL及QT自身生成的DLL。至於其它狀況本文不做討論。

鏈接庫分爲2種windows

(1)動態鏈接庫,一般有.h .lib .dll三個文件,功能實如今dll中
(2)靜態鏈接庫,一般有.h .lib二個文件,功能實如今lib中
由上能夠看出動態庫的lib和靜態庫的lib文件是不一樣的。
    若是使用生成鏈接庫的開發環境與使用鏈接庫的開發環境相同,通常不會出什麼問題,如VC寫的鏈接庫
(包括動態庫和靜態庫)還在VC中用通常不會有什麼問題的。有時候咱們須要DLL跨開發環境,如之前VC下的
DLL想在QT中用。有網友說QT不支持VC生成的靜態庫,因此只測試QT使用動態庫的狀況。
【先看VC生成的DLL】
使用VC6創建Win32 dynamic-link library工程,工程中只有兩個文件,編譯便可生成DLL和LIB
 
////////////////////////////////// MFCDLL.h  /////////////////////////////////////
#ifndef _MFCDLL_H
#define _MFCDLL_H

#ifdef __cplusplus
extern "C" {
#endif

#ifdef DLL
    // do nothing
#else
#define DLL __declspec(dllimport)
#endif

DLL void hello();
DLL int add(int a, int b);

#ifdef __cplusplus
}
#endif

#endif

 

////////////////////////////////// MFCDLL.cpp  /////////////////////////////////////
#define DLL __declspec(dllexport)
#include "MFCDLL.h"
#include <windows.h>

void hello()
{
::MessageBox(NULL, "hello world!", 
"greeting", MB_OK);
}

int add(int a, int b)
{
return a + b;
}

 

【使用QT生成DLL】使用QT創建動態庫工程,編譯便可獲得DLL(無LIB文件)函數

 

////////////////////////////////// qtdll_global.h  //////////////////////////////
#ifndef QTDLL_GLOBAL_H
#define QTDLL_GLOBAL_H

#include

#if defined(QTDLL_LIBRARY)
#  define QTDLLSHARED_EXPORT Q_DECL_EXPORT
#else
#  define QTDLLSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // QTDLL_GLOBAL_H

 

////////////////////////////////// qtdll.h  /////////////////////////////////////
#ifndef QTDLL_H
#define QTDLL_H

#include "qtdll_global.h"

class QTDLLSHARED_EXPORT QTDLL
{
public:
    QTDLL();

public:
    int add(int a, int b);
};

#endif // QTDLL_H

 

////////////////////////////////// qtdll.cpp  ///////////////////////////////////
#include "qtdll.h"


QTDLL::QTDLL()
{
}

int QTDLL::add(int a, int b)
{
    return a + b;
}

 

【QT顯式加載VC生成的DLL】測試

 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLibrary>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // DLL顯式加載,只須要DLL文件便可,不須要.H和.LIB文件
    // 須要將DLL放到可執行目錄中
    typedef void(*FUN1)();
    typedef int(*FUN2)(int, int);

    QLibrary lib("MFCDLL.dll");
    if (lib.load()) {
        qDebug() << "load ok!";

        FUN1 hello = (FUN1)lib.resolve("hello");
        FUN2 add = (FUN2)lib.resolve("add");
        if (hello) {
            qDebug() << "load hello ok!";
            hello();
        }
        if (add) {
            qDebug() << "load add ok!";
            qDebug() << add(3, 5);
        }
    } else {
        qDebug() << "load error!";
    }
}

 

【QT隱式加載VC生成的DLL】ui

 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "lib/MFCDLL.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // DLL隱式加載,只須要.DLL .H和.LIB文件
    // 1須要將DLL放到可執行目錄中
    // 2將LIB路徑設置到項目PRO文件中
    // 3將頭文件包含進來,若是不包含須要自已聲明函數原型及來源(本質與包含頭文件相同)
    hello();
    qDebug() << add(5, 6);
    qDebug() << "ok";
}

 

pro工程文件中要設置LIB文件路徑this

 

# lib文件路徑
LIBS += "F:/lib/MFC_DLL_TEST_WITH_QT_2/lib/MFCDLL.lib"

 

【QT使用QT生成的動態庫,隱式】spa

 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "lib/qtdll.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // QT使用QT生成的DLL
    // 1. 包含頭文件
    // 2. 在工程文件中指定lib路徑
    // 3. 將動態庫拷貝到可執行文件目錄
    QTDLL dll;
    qDebug() << dll.add(3, 5);
}

 

pro工程文件中的設置code

 

LIBS += "F:/lib/QT_DLL_TEST_WITH_DLL/lib/QTDLL.dll"
相關文章
相關標籤/搜索