QT 多目錄多工程 配置

2021-07-29 21:41:16 字數 4508 閱讀 2754

因為專案中我們需要把模組分的比較明確,這樣既方便大家分工合作,也方便日後模組重複使用。昨天初次摸索了一下,碰到了bug。解決了後,決定把這種開發方式和大家分享一下。

新建工程時,我們選擇 other project -> subdirs project 。這個工程即建立乙個完整的父工程,在父工程下包含很多子工程,而每個子工程可以當作乙個模組進行看待。我將這個工程命名為multi_project。

一般情況下,會自動彈出乙個建立子工程的視窗,如果不小心關掉了也沒事,我們通過這樣的方式繼續建立。我們右擊父工程的標題,選擇 new subproject…

一般我們很多子工程中,只有少部分是啟動工程,大部分是作為庫函式引用進來的,我們最好先建立作為庫函式性質的子工程,然後在建立作為啟動專案的子工程。作為庫函式的子工程請選擇 library -> c++ library

它有三個型別選項,這裡我們以 shared library為例。我將本子工程的名稱命名為 customwidget

我們開啟 新建的子工程的 .pro檔案,這裡有些知識要和大家說明下。

target = customwidget # 指定目標檔案的名稱。預設情況下包含的專案檔案的基本名稱。

template = lib

# 模板告訴qmake為這個應用生成哪種檔案。

在本部分template = lib表明將會生成lib檔案,target = customwidget表示生成的檔案名叫這個。即本模組生成的檔名將會叫customwidget.lib(在windows下)

據說在linux下會生成libcustomwidget.so

我們在這個工程的 .pro檔案中加入一行

destdir = ../bin #指定在何處放置目標檔案。
下面兩圖試著編譯了一下這個工程,來幫助大家理解destdir的用處,我們在多工程多檔案組織時有必要用到這個東西。

按照教程來做的話,父工程的pro檔案內應該只有這幾行。

template = subdirs

subdirs += \

customwidget \

我們在後面加上一行,如下方所示。

template = subdirs

subdirs += \

customwidget \

config += ordered

該字段的涵義是:使用subdirs模板時,此選項指定應該按照目錄列表的順序處理它們。到目前為止,工程應該是這樣的狀態。

我們右擊 customwidget工程名,選擇 add new。然後再選擇c++ -> c++ class

新建完成後,可以完善我們的類。

這裡有一點需要注意,我們看到列表中有個customwidget_global.h檔案。檔案內容如下「

// customwidget_global.h

#ifndef customwidget_global_h

#define customwidget_global_h

#include

#if defined(customwidget_library)

# define customwidgetshared_export q_decl_export

#else

# define customwidgetshared_export q_decl_import

#endif

#endif // customwidget_global_h

其中這個customwidgetshared_export字段需要注意,它是qt 的巨集,你新增的類,要在類的宣告中加入該字段才能夠生成在 lib檔案裡,我就是因為沒有注意到這個問題,才除錯了好長時間。

標頭檔案

#ifndef customlabel_h

#define customlabel_h

#include

#include "customwidget_global.h"

class

customwidgetshared_export

customlabel

: public

qlabel

;#endif // customlabel_h

cpp檔案

#ifndef customlabel_h

#define customlabel_h

#include

#include

"customwidget_global.h"

#include

"customlabel.h"

customlabel::customlabel(qlabel *

parent):

qlabel(parent)

標頭檔案

#ifndef customwidget_h

#define customwidget_h

#include "customwidget_global.h"

#include

#include "customlabel.h"

class

customwidgetshared_export

customwidget

:public

qwidget

;#endif // customwidget_h

cpp檔案

#include "customwidget.h"

#includecustomwidget::customwidget(qwidget *parent):

qwidget(parent)

/****************************

* 析構函式用來釋放空間

* *************************/

customwidget::~customwidget()

if(label2 == null)

if(layout == null)

}

標頭檔案

#ifndef mainwindow_h

#define mainwindow_h

#include

#include "../customwidget/customwidget.h"

class mainwindow : public qmainwindow

;#endif // mainwindow_h

cpp檔案

#include "mainwindow.h"

mainwindow::mainwindow(qwidget *parent)

: qmainwindow(parent)

mainwindow::~mainwindow()

}

我們編譯執行,可以看到效果如下圖所示。

如此,我們便實現了在多個子工程中分模組編寫。通過建立多個lib子工程,可以將模組話的任務分給更多的同伴來合作,謝謝大家

pro檔案裡字段解釋

qt下的跨目錄多工程編譯

這裡要編譯的工程包含乙個庫和乙個可執行檔案。可執行檔案依賴於庫,所以要先編譯庫,編譯後庫放在lib目錄裡面,可執行檔案放在bin目錄裡面。目錄結構如下 plain view plain copy main.cpp bin complex.pro include base.h lib src base...

多工 協程

示例 import time defwork1 while true print 正在掃地 yield defwork2 while true print 正在搬磚 yield w1 work1 w2 work2 協程肯定是併發執行 while true next w1 next w2 greenl...

多工 攜程

1 協程 1.1 協程是啥 yield import time deftask1 while true print 1 time.sleep 0.1 yield deftask2 while true print 2 time.sleep 0.1 yield defmain t1 task1 t2 ...