Qt 靜態編譯可生成無外部依賴的單一可執行文件,但配置複雜、坑點較多。本文記錄完整編譯參數、生成庫依賴關係、運行時插件加載問題及解決方案。
configure -confirm-license -opensource -developer-build -static \
-prefix D:\libraries\Qt5.3.1s \
-mp -platform win32-msvc2013 -opengl desktop -no-angle \
-nomake examples -nomake tests \
-no-icu -no-openssl \
-qt-libjpeg -qt-libpng \
-no-plugin-manifests \
-skip qtwebkit -skip qtdeclarative
關鍵選項說明:
-static:啟用靜態鏈接-developer-build:開發者構建(含調試符號)-no-icu -no-openssl:禁用 ICU 與 OpenSSL 減小體積-qt-libjpeg -qt-libpng:內建圖片格式支持-skip qtwebkit -skip qtdeclarative:跳過 WebKit 舊版 QML 模組Debug 版:
qtmaind.lib
Qt5Cored.lib
Qt5Guid.lib
Qt5Widgetsd.lib
WS2_32.LIB
imm32.lib
winmm.lib
opengl32.lib
qwindowsd.lib
Qt5PlatformSupportd.lib
Release 版:
qtmain.lib
Qt5Core.lib
Qt5Gui.lib
Qt5Widgets.lib
WS2_32.LIB
imm32.lib
winmm.lib
opengl32.lib
qwindows.lib
Qt5PlatformSupport.lib
現象:運行時報錯 Cannot load plugin qwindows 或 _WSAAsyncSelect link error、QFontDatabase link error
原因:靜態編譯時平台插件未自動鏈接,需手動導入
解決方案:在代碼中添加:
#include
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin); // 強制鏈接 Windows 平台插件
opengl32.libQt5PlatformSupport.libWS2_32.lib-no-plugin-manifests 避免清單文件衝突windeployqt 檢查依賴(靜態構建通常無需)Qt 靜態編譯雖初期投入大,但部署極其簡單,適合對啟動速度、分發便利性有要求的桌面應用場景。