配置靜態構建 qt
如下所示,執行配置的時候添加 -static
選項即可。
$skiped_modules = @("qttools""qtdoc""qttranslations""qtlanguageserver""qtdeclarative""qtquicktimeline""qtquick3d""qtgraphs""qtlocation""qtlottie""qtmqtt""qtopcua""qtquick3dphysics""qtquickeffectmaker""qtvirtualkeyboard""qtwebengine""qtwebview"
)Invoke-Expression "../configure.bat -static -skip $($skiped_modules -join ",") -prefix ${install_path}"
if ($LASTEXITCODE)
{throw "$source_path 配置失敗"
}
自行鏈接各種靜態庫
靜態鏈接 qt 時,很多依賴需要手動鏈接靜態庫解決。以下是我鏈接的
target_link_libraries(${target_name} PUBLICsetupapizsynchronizationpcre2-16double-conversionntdllversionharfbuzzb2zstdnetapi32authzws2_32winmmuserenvdwritepngmd4cd3d12dxgid3d11
)
此外還要自行鏈接 libqwindows.a
運行時仍然報錯
做了上述工作后,我的基于 qt 的程序編譯、鏈接完成了。但是運行時仍然輸出如下信息:
qt.qpa.plugin: Could not find the Qt platform plugin "windows" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
并且這個時候我把 qt 插件的那些 dll 部署到可執行文件所在目錄,或者整個 platform 拷貝到可執行文件所在目錄都不管用,仍然報錯。
不折騰了。qt 比較流行動態鏈接,我見過的基于 qt 的軟件都是動態連接的,因為攜帶了一大堆 qt 的 dll 和 platform/qwindows.dll
。此外大多數庫使用動態鏈接,如果使用靜態鏈接,但是又鏈接了這些庫,最終又變成動態鏈接了,并且可執行文件里面含了重復的 qt 代碼。
后來成功了
成功將 qt 構建為靜態庫并鏈接,成功運行。見另一篇博客:將 qt 構建為靜態庫