前言: 編譯器 Qt Creator, 系統環境 win7 64 位
1.創建共享庫:
新建文件或項目->選擇 Library 和 c++ 庫->選擇共享庫->下一步(工程名為 sharedlib)

生成的目錄結構如圖:

修改 sharedlib.h 中的代碼:
// sharedlib.h
#ifndef SHAREDLIB_H
#define SHAREDLIB_H #include "sharedlib_global.h" class SHAREDLIBSHARED_EXPORT Sharedlib// 導出類, 客戶端可直接加載 { public: Sharedlib(); void test(); }; extern "C" Q_DECL_EXPORT int add(int a, int b);//導出函數, 客戶端可用 QLibrary 加載 #endif // SHAREDLIB_H
修改 sharedlib.cpp 中的代碼:
// sharedlib.cpp
#include "sharedlib.h"
#include <QMessageBox> Sharedlib::Sharedlib() { } void Sharedlib::test() { QMessageBox::warning(0, "Sharedlib::test", "Sharedlib::test"); } int add(int a, int b) { return a+b; }
2.調用共享庫(方式一):
新建文件或項目->選擇 Application, Qt Console Application->項目名為 app1->下一步
修改客戶端程序 main.cpp 中的代碼:
// main.cpp
#include <QCoreApplication>
#include <QLibrary> #include <iostream> using namespace std; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); typedef int (*Add)(int a, int b); QLibrary mylib("sharedlib.dll"); if(!mylib.load()){// 加載 dll 失敗 cout<<"加載 sharedlib.dll 失敗!"<<endl; return -1; } Add add = (Add)mylib.resolve("add"); if(0 == add){// 加載失敗 cout<<"加載函數 add 失敗!"<<endl; return -1; } int sum = add(1,2);// 調用 add 函數 cout<<sum<<endl; return 0; return a.exec(); }
編譯 sharedlib 工程和 app1 工程-> 將生成的 sharedlib.dll 文件復制到 app1.exe 所在目錄中->運行 app1.exe 輸出 3
總結:這種調用方式需要在客戶端程序編譯時需要提供 .dll 的名字信息及其函數信息, 需要在客戶端程序運行時提供 .dll 文件, 客戶端中要寫的代碼較多.
3.調用共享庫(方式二):
新建文件或項目->選擇 Application, Qt QWidgets Application->項目名為 app2->下一步
在 app2.pro 文件最后添加如下代碼, 用來說明 sharedlib.lib 的路徑信息:
unix|win32: LIBS += -L$$PWD/../build-sharedlib-unknown-Debug/debug/ -lsharedlib
INCLUDEPATH += $$PWD/../build-sharedlib-unknown-Debug/debug DEPENDPATH += $$PWD/../build-sharedlib-unknown-Debug/debug
將 sharedlib 工程源碼目錄下的 sharedlib.h 和 sharedlib_global.h 頭文件復制到 app2 工程源碼目錄下, 將 sharedlib.dll 文件復制到 app2.exe 所在目錄中
修改客戶端程序 main.cpp 中的代碼:
// main.cpp
#include "mainwindow.h"
#include <QApplication> #include "sharedlib.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); // MainWindow w; // w.show(); Sharedlib sb; sb.test(); return a.exec(); }
編譯 app2 工程, 運行 app2.exe, 彈出如圖對話框, 表示程序正確執行:

總結: 這種調用方式需要在客戶端程序編譯時提供 .lib 和 .h 文件, 需要在客戶端程序運行時提供 .dll 文件, 客戶端中要寫的代碼較少.
4.方式一和方式二調用共享庫的異同點:
客戶端程序運行時都是只需要 .dll 文件; 編譯時有些差別, 方式一代碼較多, 但無需 .lib 和 .h 文件, 方式二代碼簡單(和調用普通類一樣), 但是需要 .lib 和 .h 文件.
創建好三個項目后的文件結構如圖:

5. 靜態鏈接庫的創建:
新建文件或項目->選擇 Library 和 c++ 庫->選擇靜態鏈接庫->下一步(工程名為 staticdlib)

修改 staticlib.h 中的代碼:
// staticlib.h
#ifndef STATICLIB_H
#define STATICLIB_H
class Staticlib { public: Staticlib(); void test(); }; #endif // STATICLIB_H
修改 staticlib.cpp 中的代碼:
// staticlib.cpp
#include "staticlib.h"
#include <iostream>
using namespace std; Staticlib::Staticlib() { cout<<"Staticlib::Staticlib"<<endl; } void Staticlib::test() { cout<<"Staticlib::test"<<endl; }
編譯工程之后會生成 staticlib.lib 文件
6. 調用靜態鏈接庫:
新建文件或項目->選擇 Application, Qt Console Application->項目名為 app->下一步
在 app.pro 文件最后添加如下代碼, 用來說明 staticlib.lib 的路徑信息:
unix|win32: LIBS += -L$$PWD/../build-staticlib-unknown-Debug/debug/ -lstaticlib
INCLUDEPATH += $$PWD/../build-staticlib-unknown-Debug/debug DEPENDPATH += $$PWD/../build-staticlib-unknown-Debug/debug
將 staticlib 工程目錄下的 staticlib.h 復制到 app 工程目錄下
修改客戶端程序 main.cpp 中的代碼:
// main.cpp
#include <QCoreApplication>
#include "staticlib.h"
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Staticlib sb; sb.test(); return a.exec(); }
編譯 app 工程, 生成 app.exe 文件, 運行 app.exe 文件, 輸出如圖:

總結: 客戶端程序編譯時需要 .h 和 .lib 文件, 運行時無需其它文件.
7. 共享庫(動態鏈接庫)和靜態鏈接庫的異同點:
異: 調用共享庫的客戶端運行時需要 .dll 文件, 靜態鏈接庫的不需要;
庫程序發生改變時, 共享庫客戶端只需更新 .dll 文件, 靜態鏈接庫客戶端需要重新編譯.
同: 都可用於多模塊共同開發程序
注: 本文是原創文章, 歡迎咨詢或提出修改意見, 聯系方式QQ:1871046323@qq.com
源碼地址: https://github.com/ZhangShuaiH/QT/tree/master/demos/lib
