系統1:ThinkPad T570、Windows10、QT5.12.2(Qt Creater 4.8.2)
一、動態庫.dll的創建和調用
1.在qtcreater中按如下步驟創建動態庫,動態庫名為mydll:
選擇Library項目,C++庫
選擇共享庫:
選擇qt自帶的kit:
在工程中自動生成的mydll.pro文件里內容如下:
#-------------------------------------------------
#
# Project created by QtCreator 2019-04-05T11:14:57
#
#-------------------------------------------------
QT -= gui #在選擇需要的模塊時,我只選用了QtCore,沒有使用QtGui
TARGET = mydll #我配置的動態庫的名字:mydll
TEMPLATE = lib #生成庫時該字段為lib;生成執行文件時為:app
DEFINES += MYDLL_LIBRARY #將MYDLL_LIBRARY添加為編譯時的預處理器宏,在share_global.h中使用
# 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.0
SOURCES += \
mydll.cpp
HEADERS += \
mydll.h \
mydll_global.h
unix {
target.path = /usr/lib
INSTALLS += target
}
mydll.h文件內容如下編寫,其中我只添加了sum和squaresum兩個函數定義,其他的為自動生成:
#ifndef MYDLL_H
#define MYDLL_H
#include "mydll_global.h"
class MYDLLSHARED_EXPORT Mydll
{
public:
Mydll();
int sum(int a,int b);
int squaresum(int a,int b);
};
#endif // MYDLL_H
mydll_global.h文件為自動生成,在此不多描述。
mydll.cpp文件中如下編寫:
#include "mydll.h"
Mydll::Mydll()
{
}
int Mydll::sum(int a, int b)
{
return (a+b);
}
int Mydll::squaresum(int a, int b)
{
return (a*a+b*b);
}
隨后按Ctrl+B鍵來構建該項目,構建成功后會在工程文件所在的同級目錄下生成build-mydll-Desktop_Qt_5_12_2_MinGW_64_bit-Debug文件夾,該文件夾內有libmydll.a,mydll.dll和mydll.o三個文件,我們需要用的是mydll.dll。
2.在qtcreater中按如下步驟創建使用動態庫的工程,工程取名為UseLib。



將mydll.h和mydll_global.h兩個文件從mydll項目文件夾中拷貝到UseLib工程源文件夾下(F:\QTCode\TestCode\TestLib\uselib\UseLib)
在mainwindow.h文件中添加動態庫的頭文件#include “mydll.h”,並定義一個動態庫類對象Mydll mylib,代碼如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mydll.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
Mydll mylib;
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
將mydll項目中生成的mydll.dll文件放到UseLib工程源文件夾下。
在UseLib.pro文件的最后一行添加如下代碼,將動態庫包含進當前項目:
LIBS +=-L$$PWD -lmydll # $$PWD表示當前路徑,mydll根據生成動態庫的工程的mydll.pro里面的TARGET = mydll得到
- 1
mainwindow.ui如下圖布局:
mainwindow.cpp如下編寫代碼:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
int a = ui->lineEdit1->text().toInt();
int b = ui->lineEdit2->text().toInt();
int nsum = mylib.sum(a,b);
QString str = QString::number(nsum);
ui->lineEditsum->setText(str);
}
運行結果如下:
二、靜態庫.a的創建和調用
步驟與動態庫幾乎相同,除了如下幾點:
1.創建靜態庫的名字為mylib,選擇類型為“靜態鏈接庫”,如下:
2.靜態庫里面還是一個sum(int a,int b)的方法,靜態庫創建完之后,在構造出的“build-mylib-Desktop_Qt_5_12_2_MinGW_64_bit-Debug\debug”文件夾下會生成libmylib.a和mylib.o文件,我們要用的是libmylib.a文件。
3.創建使用靜態庫的工程,取名為uselib,將libmylib.a和mylib.h拷貝到項目源文件路徑下和構造路徑下
4.在uselib.pro中的最后一行添加
LIBS +=-L$$PWD -lmylib
- 1
5.在mainwindow.h中添加#include “mylib.h”,並定義一個類對象Mylib mylib;
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mylib.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
Mylib mylib;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
6.在mainwindow.cpp中添加如下代碼:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
int a = ui->lineEdit1->text().toInt();
int b = ui->lineEdit2->text().toInt();
int nsum = mylib.sum(a,b);
QString str = QString::number(nsum);
ui->lineEditsum->setText(str);
}
運行結果如下:

注:linux下按照如上流程創建和調用靜態庫 親測成功過,用的系統為
硬件:NVIDIA Tegra X2
系統:Ubuntu 16.04LTS
QT版本:QT5.5.1(Qt Creater 3.5.1)
但在linux下調用動態庫的時候會失敗,具體后續我會再研究一下。
