700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > qt-制作生成dll动态链接库实例

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

时间:2020-05-27 10:17:36

相关推荐

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

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;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。