目录

 

1.创建共享动态库

1.1 新建工程,选择"Library"->C++库,点击"Choose"

1.2 选择类型"共享库",下拉列表里有“共享库”,“静态链接库”,“Qt plugin”,本文选择“共享库”。然后输入动态库名称,此例我使用“Library”,点击"下一步"

1.3 选择构建套件,本例选择mingw 32bit,点击"下一步"

1.4 选择动态库需要的模块,本例中默认选择,点击"下一步"

1.5 详情提示,这里可以修改类名和头文件源文件名,点击"下一步"

1.6 项目管理界面,保持默认即可,点击"完成"按钮

1.7 上一步点击"完成后",会生成动态库程序

1.8 点击编译按钮

2.使用共享动态库

2.1 创建可执行程序

2.2 设置头文件路径和lib库路径

2.2.1 生成工程后,我们首先把动态库的头文件copy过来。

2.2.2 把lib库和动态库也要copy过来

2.2.3 打开testdll.pro,设置动态库头文件和lib库的路径

2.3 使用函数接口

2.4 将动态库copy到可执行文件路径下

end


1.创建共享动态库

1.1 新建工程,选择"Library"->C++库,点击"Choose"

qt for android打包动态库 qt 生成动态库_动态库

1.2 选择类型"共享库",下拉列表里有“共享库”,“静态链接库”,“Qt plugin”,本文选择“共享库”。然后输入动态库名称,此例我使用“Library”,点击"下一步"

qt for android打包动态库 qt 生成动态库_头文件_02

1.3 选择构建套件,本例选择mingw 32bit,点击"下一步"

qt for android打包动态库 qt 生成动态库_qt for android打包动态库_03

1.4 选择动态库需要的模块,本例中默认选择,点击"下一步"

qt for android打包动态库 qt 生成动态库_头文件_04

1.5 详情提示,这里可以修改类名和头文件源文件名,点击"下一步"

qt for android打包动态库 qt 生成动态库_#include_05

1.6 项目管理界面,保持默认即可,点击"完成"按钮

qt for android打包动态库 qt 生成动态库_动态库_06

1.7 上一步点击"完成后",会生成动态库程序

生成的目录结构如下图:

qt for android打包动态库 qt 生成动态库_#include_07

其中library.h和library_global.h为头文件,我们可以把library_global.h文件的内容copy到library.h中,这样我们在使用动态库时只需要包含library.h即可。

library.pro

#-------------------------------------------------
#
# Project created by QtCreator 2019-08-21T12:56:02
#
#-------------------------------------------------

QT       -= gui

TARGET = library
TEMPLATE = lib

DEFINES += LIBRARY_LIBRARY

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += library.cpp

HEADERS += library.h\
        library_global.h

unix {
    target.path = /usr/lib
    INSTALLS += target
}

library_global.h

#ifndef LIBRARY_GLOBAL_H
#define LIBRARY_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(LIBRARY_LIBRARY)
#  define LIBRARYSHARED_EXPORT Q_DECL_EXPORT
#else
#  define LIBRARYSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // LIBRARY_GLOBAL_H

library.h

原始的library.h中只有一个空的类,我在这个文件中添加了几个方法,有普通函数接口

double LIBRARYSHARED_EXPORT Divide(int a,int b);

有成员函数接口 

int Multi(int a ,int b);

静态成员函数接口

static int Add(int a,int b);
static int Del(int a,int b);
#ifndef LIBRARY_H
#define LIBRARY_H

#include "library_global.h"

//函数接口
double LIBRARYSHARED_EXPORT Divide(int a,int b);

//类接口
class LIBRARYSHARED_EXPORT Library
{

public:
    Library();
    //类静态成员函数接口
    static int Add(int a,int b);
    static int Del(int a,int b);
    //类成员函数接口
    int Multi(int a ,int b);
};

#endif // LIBRARY_H

library.cpp

#include "library.h"

Library::Library()
{
}

int Library::Add(int a, int b)
{
    return a+b;
}

int Library::Del(int a, int b)
{
    return a - b;
}

int Library::Multi(int a, int b)
{
    return a*b;
}

double Divide(int a,int b)
{
    return (double)a/(double)b;
}

1.8 点击编译按钮

qt for android打包动态库 qt 生成动态库_#include_08

生成了三个文件liblibrary.a,liblibrary.dll,liblibrary.o,其中liblibrary.a,liblibrary.dll是使用动态库需要用到的。到此完成了共享动态库的创建。下面开始介绍共享动态库的使用。

2.使用共享动态库

2.1 创建可执行程序

qt for android打包动态库 qt 生成动态库_动态库_09

qt for android打包动态库 qt 生成动态库_头文件_10

qt for android打包动态库 qt 生成动态库_#include_11

qt for android打包动态库 qt 生成动态库_qt for android打包动态库_12

点击完成,生成工程。

2.2 设置头文件路径和lib库路径

qt for android打包动态库 qt 生成动态库_qt for android打包动态库_13

2.2.1 生成工程后,我们首先把动态库的头文件copy过来。

qt for android打包动态库 qt 生成动态库_头文件_14

2.2.2 把lib库和动态库也要copy过来

qt for android打包动态库 qt 生成动态库_qt for android打包动态库_15

2.2.3 打开testdll.pro,设置动态库头文件和lib库的路径

QT += core
QT -= gui

CONFIG += c++11

TARGET = dlltest
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
HEADERS += $$PWD/library/include/library_global.h \
            $$PWD/library/include/library.h

LIBS += $$PWD/library/lib/win32/release/liblibrary.a

2.3 使用函数接口

main.cpp

#include <QCoreApplication>
#include <QDebug>
#include "library/include/library.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int num1 = 10;
    int num2 = 3;
    qDebug()<<"类静态成员函数接口"<<Library::Add(num1,num2);
    qDebug()<<"类静态成员函数接口"<<Library::Del(num1,num2);
    Library library;
    qDebug()<<"类成员函数接口"<<library.Multi(num1,num2);
    qDebug()<<"普通函数接口"<<Divide(num1,num2);
    return a.exec();
}

2.4 将动态库copy到可执行文件路径下

qt for android打包动态库 qt 生成动态库_qt for android打包动态库_16

2.5 动态加载动态库,需要注意导出的函数名,可使用dumpbin查看

#include <QCoreApplication>
#include <QDebug>
#include <QString>
#include <QLibrary>
typedef int (*myfun)(int,int);//定义函数形式
typedef double (*myfun2)(int,int);
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QLibrary test_dll("library.dll");//加载动态库
    if(test_dll.load())
    {
        myfun2 fun2 = (myfun2)test_dll.resolve("Divide");//获取dll的函数
        if(fun2)
        {
            double result = fun2(10,3);
            qDebug()<<result;
        }
    }
    test_dll.unload();
    return a.exec();
}