項目結構
整個工程由一個主程序構成和一個模塊構成(dll)。整個工程的結構目錄如下
Define.priMyProject.proMyProject.pro.user
+---bin
+---MainProgrammain.cppMainProgram.proMainProgram.pro.userwidget.cppwidget.hwidget.ui
+---MathDllMathDll.proMathDll.pro.userMyMath.cppMyMath.h
qmake文件介紹
1 MyProject.pro
TEMPLATE = subdirs #多工程項目
CONFIG += ordered #指定編譯順序
SUBDIRS = MathDll \ #模塊(dll)MainProgram #主工程
2 Define.pri
win32 {CONFIG(release, debug|release){contains(QT_ARCH, i386) {BIN_PATH=release_x86_} else {BIN_PATH=release_x64_}} else {contains(QT_ARCH, i386) {BIN_PATH=debug_x86_} else {BIN_PATH=debug_x64_}}
}DESTDIR = $$PWD/bin/$$BIN_PATH #生成的二進制文件(可執行文件或者庫)的目標位置,放在pri文件中,在有pro文件包含這樣就不用重復寫
3 MathDll.pro
QT -= guiTEMPLATE = libCONFIG += 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
DEFINES += MATHDLL_LIBRARY #使用預定義來控制是導出函數符號到dll還是導出dll中的函數符號# 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.0include($$PWD/../Define.pri) #引入pri文件SOURCES += \MyMath.cpp \MyMath.h
#拷貝頭文件到指定目錄,以便其它程序能夠使用該dll
QMAKE_POST_LINK += xcopy /y/F \"$$PWD/MyMath.h\" \"$$PWD/../bin/includePath\\\" $$escape_expand(\\n\\t)# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
4 MainProgram.pro
#-------------------------------------------------
#
# Project created by QtCreator 2023-12-09T17:29:33
#
#-------------------------------------------------QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = MainProgram
TEMPLATE = app# The following define makes your compiler emit warnings if you use
# any feature of Qt which has 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.0include($$PWD/../Define.pri) #引入pri文件
LIBS += -L$$PWD/../bin/$$BIN_PATH #設置搜索鏈接動態庫的目錄CONFIG += c++11INCLUDEPATH += $$PWD/../bin/includePath #設置搜索頭文件目錄
LIBS += -lMathDll #鏈接庫文件SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
注意 :
LIBS += -L$$PWD/../bin/$$BIN_PATH
這句話是很重要的,在程序編譯鏈接時會使用到動態庫的 lib
文件,用于生成程序。而默認鏈接的位置是 MainProgram
目錄下,如果沒有該語句就會提示找不到 MathDll.lib
文件,可執行程序生成失敗。
源文件分析
1 MyMath.h
#ifndef MYMATH_H_
#define MYMATH_H_
//包含QObject才能識別 Q_DECL_EXPORT Q_DECL_IMPORT
#include <QObject>
//預定義來控制導入導出符號
#if defined (MATHDLL_LIBRARY)
#define MATHDLL_LIBRRY_EXPORT Q_DECL_EXPORT
#else
#define MATHDLL_LIBRRY_EXPORT Q_DECL_IMPORT
#endifclass MATHDLL_LIBRRY_EXPORT MyMath{
public:MyMath();~MyMath();int add(int a, int b);
};#endif
2 widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "MyMath.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}void Widget::on_pushButton_clicked()
{int a = ui->lineEdit->text().toInt();int b = ui->lineEdit_2->text().toInt();MyMath myMath;int sum = myMath.add(a, b); //使用動態鏈接庫中的函數ui->label->setText(ui->label->text() + QString::number(sum));
}