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"
相关文章
相关标签/搜索