配置静态构建 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 构建为静态库