Qt之建立並使用靜態鏈結庫

2021-08-20 21:20:25 字數 1829 閱讀 5630

繼上一節

qt之建立並使用共享庫

之後,關於動態鏈結庫和靜態鏈結庫已經有了更深入的認識,所以這裡不再贅述,下來我們一起看看如何建立與使用靜態鏈結庫。

建立步驟與共享庫一致,唯一的區別是庫型別選擇:

靜態鏈結庫。

qt       += core gui widgets

target = staticlibrary

template = lib

config += staticlib

headers += staticlibrary.h

sources += staticlibrary.cpp

template = lib 

定義config += staticlib  定義匯出庫的型別為靜態鏈結庫

#ifndef staticlibrary_h

#define staticlibrary_h

#include

class staticlibrary : public qwidget

;int add(int a, int b);

#endif // staticlibrary_h

#include "staticlibrary.h"

#include

#include

#include

staticlibrary::staticlibrary(qwidget *parent)

: qwidget(parent)

void staticlibrary::onclicked()

void staticlibrary::updatebackground()

int staticlibrary::subtract(int a, int b)

int add(int a, int b)

執行qmake,然後構建,會生成乙個staticlibrary.lib檔案。這樣我們的建立靜態鏈結庫就徹底完成了。

下面我們來看看如何去應用這個共享庫:

首先新建乙個測試工程,然後新建乙個資料夾staticlibrary,並將將剛才生成的檔案(staticlibrary.lib、staticlibrary.h)拷貝到該目錄下(也可以不拷貝,這裡為了更清楚地說明路徑及引用)。

teststaticlibrary.pro

qt       += core gui widgets

target = teststaticlibrary

includepath += ./staticlibrary

#-l$$pwd/staticlibrary -lstaticlibrary

libs += $$pwd/staticlibrary/staticlibrary.lib

headers += staticlibrary/staticlibrary.h

sources += main.cpp

main.cpp

#include "staticlibrary/staticlibrary.h"

#include

#include

int main(int argc, char *argv)

這樣我們就完成了靜態庫的建立與使用,是不是比動態庫更簡單呢!o(∩_∩)o哈哈~

效果如下:

一去丶二三里

原文:

使用Qt建立動態和靜態鏈結庫

靜態鏈結庫是將函式和資料編譯成的乙個二進位制檔案,linux下的靜態鏈結庫是 a檔案,而在windows下的靜態鏈結庫是 lib檔案。編譯器在連線的時候會恢復靜態庫檔案中的函式和資料,並將它們和應用程式中的其它模組組合在一起生成可執行檔案,因此,體積比較大。在qt中建立靜態庫檔案的主要步驟如下 1 ...

使用Qt建立動態和靜態鏈結庫

靜態鏈結庫是將函式和資料編譯成的乙個二進位制檔案,linux下的靜態鏈結庫是 a檔案,而在windows下的靜態鏈結庫是 lib檔案。編譯器在連線的時候會恢復靜態庫檔案中的函式和資料,並將它們和應用程式中的其它模組組合在一起生成可執行檔案,因此,體積比較大。在qt中建立靜態庫檔案的主要步驟如下 1 ...

製作靜態鏈結庫並使用

1 製作靜態鏈結庫 第一步 demo.c demo.h demo.c include void func1 void int func2 int a,int b demo.h void func1 void int func2 int a,int b 第二步 製作靜態庫 gcc c demo.c o...