qt-制作生成dll动态链接库实例

176 篇文章 55 订阅
22 篇文章 2 订阅

debug模式下体积过大的话,选择release编译会缩小很大一部分体积。

选择创建qt库项目,根据提示选择相应配置:

Type:Shared Libray;Statically linked Library;QT Plugin

Class name:dlllibtest

QT module:None;Core;Gui;Widgets(对于一般函数不引用qt库的话封装经测试选择None和Core一样的效果包括生成的dll大小等)

Head file:默认为类名.h(可改)

Sorce file:默认为类名.cpp(可改)

配置好后会自动生成3个文件,可以按如下所示编写dll代码

1.pro文件

QT -= gui

TEMPLATE = lib
DEFINES += DLLLIBTEST_LIBRARY

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has 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 it uses 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 += \
    dlllibtest.cpp

HEADERS += \
    DllLibTest_global.h \
    dlllibtest.h

# Default rules for deployment.
unix {
    target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target
2._global.h文件(将此文件里的声名复制到实现的头文件中,QT module:None;Core两种模式下内容有些差别但复制方式类似)

#ifndef DLLLIBTEST_GLOBAL_H
#define DLLLIBTEST_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(DLLLIBTEST_LIBRARY)
#  define DLLLIBTEST_EXPORT Q_DECL_EXPORT
#else
#  define DLLLIBTEST_EXPORT Q_DECL_IMPORT
#endif

#endif // DLLLIBTEST_GLOBAL_H
3.dlllibtest.h文件

#ifndef DLLLIBTEST_H
#define DLLLIBTEST_H

#include "DllLibTest_global.h"

#if defined(DLLLIBTEST_LIBRARY)
#  define DLLLIBTEST_EXPORT Q_DECL_EXPORT
#else
#  define DLLLIBTEST_EXPORT Q_DECL_IMPORT
#endif
extern "C"{
DLLLIBTEST_EXPORT int Sum(int a,int b);


extern "C" class  DLLLIBTEST_EXPORT  DllLibTest
{
public:
    DllLibTest();
};

DLLLIBTEST_EXPORT int Sum2(int a,int b);
}

#endif // DLLLIBTEST_H
4.dlllibtest.cpp文件

#include "dlllibtest.h"

DllLibTest::DllLibTest()
{

}
 int Sum(int a,int b)
{
    return  a+b;
}
 int Sum2(int a,int b)
{
    return  a+b;
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值