qt-制作生成dll动态链接库实例
debug模式下體積過大的話,選擇release編譯會縮小很大一部分體積。
選擇創(chuàng)建qt庫項目,根據(jù)提示選擇相應(yīng)配置:
Type:Shared Libray;Statically linked Library;QT Plugin
Class name:dlllibtest
QT module:None;Core;Gui;Widgets(對于一般函數(shù)不引用qt庫的話封裝經(jīng)測試選擇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文件(將此文件里的聲名復(fù)制到實現(xiàn)的頭文件中,QT module:None;Core兩種模式下內(nèi)容有些差別但復(fù)制方式類似)
#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;
}
?
總結(jié)
以上是生活随笔為你收集整理的qt-制作生成dll动态链接库实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入浅出 消息队列 ActiveMQ
- 下一篇: qt-信号和槽的连接写法