如何評價Qt Lite Project?

Introducing the Qt Lite project


Qt到了5.8版本,終於迎來了一個重要功能,就是QtLite。

此功能可謂是千呼萬喚始出來,飽受詬病的體積問題,終於可以在一定程度上得到解決。

在這個回答里,先是介紹QtLite,然後如何使用以及使用效果,最後是我的評價。

那麼QtLite到底是什麼呢,按照官方原話,是

Configurability (Qt Lite Project)
Qt 5.8 comes with a rewritten configuration system that allows for easy customization of Qt builds. The main focus of this feature is for the Device Creation, but it can also be used to tailor a Qt build for mobile or desktop platforms. The new configuration system allows removing individual pieces of functionality and APIs from Qt, creating a more lightweight set of libraries for deployment.

也就是說,QtLite實質上是一個配置系統。

話說我最初以為這是一個單獨的工具,可以直接縮減應用的體積。然而並不是。

我這裡主要說下後半段,也就是 removing individual pieces of functionality and APIs from Qt ,這句話的意思就是我們可以直接去除Qt模塊中的功能和API了,這是縮減體積最重要的部分。

什麼叫去除功能和API呢。我舉個例子

Qt里有一個類,叫QClipboard,就是剪切板相關的功能。但是很多人出於某些原因,不用這個,或者用了系統的API。那麼這樣的話,這個QClipboard就沒有任何存在的意義了,我們就可以直接去掉它,以縮減庫體積,最終縮減應用的體積。

那麼如何做到這一點呢。首先我們要重新編譯Qt的源碼,然後在配置的時候,添加特定的參數,來進行配置,去除掉這個功能。

參數很簡單,就是 -no-feature-clipboard

配置好後,重新編譯Qt,就可以了。

目前Qt有一個feature列表,裡面記錄了Qt有哪些feature,有100多個。我的理解就是這裡的feature都是可以去掉的,這個列表存放的位置在源碼的 qtbasesrccorelibglobalqfeatures.txt

內容如下:

不過呢,在我的實際使用中,我發現這個列表裡的並非都可以去掉,我只能說一大半是可以去掉的。要我說怎麼區分哪些可以,哪些不可以。首先是看Requires,模塊間是相互依賴的,被Requires的模塊去掉的話,Requires它的模塊就會沒法正常編譯。在Requires都得到保證的情況下,也不是說所有模塊都可以去掉,有時候編譯會報錯,那麼只能把去掉的feature加回去。

比如說,遇到了這個

這裡說QSettings不是一個類型。的確,我去掉了settings這個feature。看起來是其他的模塊,使用了QSettings。

(貌似是gui那裡用了core里的這個feature?那我如果不用gui是不是可以仍然去掉這個feature呢?具體還沒嘗試)

但是settings這個feature在列表裡,沒有Requires其他feature,其他feature也沒有Requires它,有圖為證。

不過,有什麼辦法呢,無力修改Qt源碼,只能再把settings加回去。

我試了N種配置,編譯了整整1天,真的好心累,終於調出來一個可以編譯過,然後去掉儘可能多的模塊配置。如下:

configure.bat -static -release -platform win32-g++ -no-opengl -prefix "C:QtQt5.8.0_MinGW_static" -opensource -confirm-license -make libs -nomake tools -nomake examples -nomake tests -skip qt3d -skip qtandroidextras -skip qtcanvas3d -skip qtcharts -skip qtconnectivity -skip qtdatavis3d -skip qtdeclarative -skip qtdoc -skip qtgamepad -skip qtgraphicaleffects -skip qtimageformats -skip qtlocation -skip qtmacextras -skip qtmultimedia -skip qtnetworkauth -skip qtpurchasing -skip qtquickcontrols -skip qtquickcontrols2 -skip qtscript -skip qtscxml -skip qtsensors -skip qtserialbus -skip qtserialport -skip qtspeech -skip qtsvg -skip qttools -skip qttranslations -skip qtvirtualkeyboard -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtwebview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns -no-feature-texthtmlparser -no-feature-textodfwriter -no-feature-concurrent -no-feature-effects -no-feature-sharedmemory -no-feature-systemsemaphore -no-feature-im -no-feature-process -no-feature-dom -no-feature-filesystemmodel -no-feature-filesystemwatcher -no-feature-graphicsview -no-feature-graphicseffect -no-feature-sizegrip -no-feature-calendarwidget -no-feature-printpreviewwidget -no-feature-keysequenceedit -no-feature-colordialog -no-feature-filedialog -no-feature-fontdialog -no-feature-printpreviewdialog -no-feature-progressdialog -no-feature-inputdialog -no-feature-errormessage -no-feature-wizard -no-feature-datawidgetmapper -no-feature-imageformat_bmp -no-feature-imageformat_ppm -no-feature-imageformat_xbm -no-feature-imageformat_png -no-feature-imageformat_jpeg -no-feature-image_heuristic_mask -no-feature-image_text -no-feature-colornames -no-feature-cups -no-feature-paint_debug -no-feature-freetype -no-feature-translation -no-feature-codecs -no-feature-big_codecs -no-feature-iconv -no-feature-ftp -no-feature-udpsocket -no-feature-networkproxy -no-feature-socks5 -no-feature-networkdiskcache -no-feature-bearermanagement -no-feature-completer -no-feature-fscompleter -no-feature-desktopservices -no-feature-mimetype -no-feature-systemtrayicon -no-feature-undocommand -no-feature-undostack -no-feature-undogroup -no-feature-undoview -no-feature-statemachine -no-feature-gestures -no-feature-dbus

感覺去掉的東西還不是很多,主要是去掉後各種報錯,以後新版本再跟進下。

至於效果,我們主要看core、gui和widgets。

我使用的版本是Windows下,MinGW,5.8.0,開源版

在原版中,靜態編譯後,分別是

在使用QtLite,靜態編譯後,分別是

可見體積的確縮小了。

接下來試下QtWidgets工程,我使用QtCreator創建一個默認的工程,然後放進去一個HelloQtLite字樣的QLabel。

編譯出來的exe:

原版 + 靜態編譯:

QtLite + 靜態編譯:

16MB縮減到11MB,體積還是很可觀的縮小了

再rar壓縮一下:

看來QtLite配合靜態編譯還有rar壓縮,簡單的QtWidgets程序可以縮減到4MB以內,很給力。

最後,都丟到一個純凈的Win7,正常運行

對了,因為有人提到過Qt模塊劃分的問題,我也去看了下,在我使用的源碼里,Qt一共有38個大分組,比如說qtbase,qt3d等等。QtLite目前主要優化了qtbase這一個分組,也是最常用的一個。不使用QtLite的情況下,全部編譯完成後,一共會得到60個庫。這一次我編譯後,通過skip(跳過編譯),還有QtLite,一共可以去掉42個庫,剩下18個。

最後,要我評價QtLite的話,我認為是:
這是一個很好的功能,因為在這個功能上我們可以以很低的使用成本裁剪Qt,甚至不需要直接接觸到代碼。允許開發者們定製自己想要的模塊,拋棄不必要的功能。
這在嵌入式上會有較大的意義,QtLite應該也是主要給嵌入式開發的特性。
在桌面上我覺得意義有限,但是的確很多人會去糾結這個體積問題。
另外,作為第一個擁有QtLite的Qt版本,也就是5.8,固然存在一些問題,希望Qt能在將來的版本中解決。或者允許去除更多模塊,進一步縮減體積。

PS:其實官方有一個小工具的,叫Qt Configuration Tool。就是QtLite可視化配置工具。
http://doc.qt.io/QtEnterpriseEmbedded/qt-configuration-tool.html
不過我沒找到如何下載的地方,看URL鏈接,我猜測是只有嵌入式設備授權的的Qt企業版,才有這個工具。殘念ing。。。

PS2:知乎你能不能別這麼坑,圖片怎麼動不動就掛掉,我已經重新恢復2次了


估計再怎麼減肥也不會少於3MB。


一個本來應該早就出現,但是拖到現在都沒有出現的項目。


把一個庫做成跟一個系統一樣複雜,不知道究竟是進步還是退步。這裡粒度的把握究竟在哪裡呢?到底是要耦合還是要鬆散呢?既然可以模塊劃分,那為何不幹脆就劃分成一個個小的庫?或者既然做不到正交劃分,那麼使用上會不會給用戶選擇帶來很多困難?最終還是大多數都一股腦的塞進去,畢竟誰會在乎APP的哪一點空間?而真正嵌入式場合,做不到1M這個水平,那麼再大些又何妨?

這個回答原本是在@JasonWong 回答下一個評論,既然有人贊,我也不在乎把自己的意見直接表達出來獻醜了,僅供參考。有關技術細節請參考@JasonWong的回答。


你好,請問用了 -no-feature 那麼該功能就不能用了嗎? 如果A工程為了最小化 -no-feature 了很多功能,編譯了一個特別版的QT, 如果B工程需要某些功能 又加進一些功能,那麼又要編譯一個特別版的QT. 那樣N多工程就要N多版本的自編譯QT? 請問QT lite project是這樣的嗎


Qt 5.8 Alpha released - Qt Blog

已經發布了。


出來估計還得等很久!


早該搞了……


減減肥,挺好的,現在一個Hello都那麼大,受不鳥


推薦閱讀:

什麼時候用異常,什麼時候用斷言?
以後想做大型遊戲(至少是端游,不是手游),不知道是不是一定需要精通C++或者熟練?
為什麼老師不推薦用《C++ Primer》作為教材?
C++ 在哪些設計原則的指導下,變得越來越複雜?
C++ 程序員怎麼寫簡歷?

TAG:Linux | QtC開發框架 | C |