windows Qt静态链接和部署
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1011197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt static linking and deployment
提问by
I am trying to deploy(release to public) a simple qt application I made recently, but got stuck at static linking qt libs.
我正在尝试部署(向公众发布)我最近制作的一个简单的 qt 应用程序,但卡在静态链接 qt 库上。
I followed the guide on qt docs to re-build qt and my app statically. But the release build still require qtgui / qtcore dll for no apparent reasons, I wonder if anyone has seen this kind of problems before ? Or even better, has successfully resolved it ?
我按照 qt docs 上的指南静态地重新构建 qt 和我的应用程序。但是由于没有明显原因,发布版本仍然需要qtgui / qtcore dll,我想知道以前是否有人见过这种问题?或者更好的是,已经成功解决了吗?
回答by Charles Burns
I wrote a guide to static linking
and How to build Qt static with multiple compilers and keep it small
(because it can get pretty big, especially for simple programs). You may also want to check out the BitRock installer, which is free for open source projects.
(因为它可以变得非常大,特别是对于简单的程序)。您可能还想查看 BitRock 安装程序,它对开源项目是免费的。
In short, it turns out to be a little more complex if you are using anything Qt thinks of as a plugin, such as support for most image types (JPEG, GIF) or databases. For example, if you want to include support for Oracle DBMS and GIF images for your icons, you add the following to your .PRO file:
简而言之,如果您使用 Qt 认为是插件的任何东西,例如对大多数图像类型(JPEG、GIF)或数据库的支持,结果会稍微复杂一些。例如,如果您想为图标包含对 Oracle DBMS 和 GIF 图像的支持,请将以下内容添加到 .PRO 文件中:
QTPLUGIN += qsqloci qgif
CONFIG += static
You will then need to:
然后你需要:
#include <QtPlugin>
in your project, and import any plugins used. You need to change these settings back order to get it to compile with dynamic linking again (like when debugging or adding features), though this can be easily automated. There are also considerations when building the Qt libraries for use with static linking, though the Qt instructions will at least get you started.
在您的项目中,并导入使用的任何插件。您需要更改这些设置以使其再次使用动态链接进行编译(例如在调试或添加功能时),尽管这可以轻松实现自动化。在构建用于静态链接的 Qt 库时也有一些注意事项,尽管 Qt 说明至少会让您入门。
回答by Reinstate Monica
With Qt 5.5, things are quite easy. There are following orthogonal settings that you pass to configure
when building Qt:
有了 Qt 5.5,事情就变得很简单了。configure
在构建 Qt 时,您传递给以下正交设置:
Do you want a static Qt library?
-static
option should be passed toconfigure
Do you want the build of Qt, and of your application, to use a static C++ runtime?
-static-runtime
option should be passed toconfigure
Do you want XP targeting?
-target xp
option should be passed toconfigure
Additionally, follow the instructions from this blog post.
Qt Creator didn't support XP targeting automagically at least until v.3.5.0 since it doesn't set up the environment for the build tools properly. You have to modify the build environment manually per the blog post.
回答by Legolas
Also, be aware that your static build will still link to the visual studio runtimes dynamically!
另外,请注意您的静态构建仍将动态链接到 Visual Studio 运行时!
See this faq(internet archive link, in case the link goes away) :
请参阅此常见问题解答(互联网档案链接,以防链接消失):
Why does a statically built Qt use the dynamic Visual Studio runtime libraries ? Do I need to deploy those with my application ?
Qt is built using the -MD(d) switch, which links against the dynamic C/C++ runtime libraries. This is necessary as we have experienced memory problems when using anything but the -MD(d) flag, and in general, it is recommended to use. You should not alter this flag yourself for your application, because it conflicts with how the Qt library is built if you change the flag to -MT. You should not change it for Qt either, since it is likely to cause problems.
Qt is still built statically when using the -static option though, meaning you do not need to distribute the Qt dlls when deploying your application. You will have to distribute the C runtimes though (if they don't already exist on the target machine), see our deployment documentation http://doc.qt.io/qt-5/windows-deployment.html#application-dependencies.
为什么静态构建的 Qt 使用动态 Visual Studio 运行时库?我需要在我的应用程序中部署它们吗?
Qt 是使用 -MD(d) 开关构建的,它链接到动态 C/C++ 运行时库。这是必要的,因为我们在使用除 -MD(d) 标志之外的任何东西时都遇到了内存问题,一般情况下,建议使用。您不应该为您的应用程序自行更改此标志,因为如果将标志更改为 -MT,它会与 Qt 库的构建方式相冲突。您也不应该为 Qt 更改它,因为它可能会导致问题。
但是,在使用 -static 选项时,Qt 仍然是静态构建的,这意味着您在部署应用程序时不需要分发 Qt dll。您将不得不分发 C 运行时(如果它们在目标机器上尚不存在),请参阅我们的部署文档http://doc.qt.io/qt-5/windows-deployment.html#application-dependencies.
回答by Darklighter
msys2has a pre-built static Qt5 package
e.g. pacman -S mingw-w64-qt5-static
例如 pacman -S mingw-w64-qt5-static
Currently, to also get all dependencies of Qt statically linked, with CMake, you need to add:list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .a .lib)
before calling find_package(Qt5
…
目前,还需要静态链接 Qt 的所有依赖项,使用 CMake,您需要添加:list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .a .lib)
在调用之前find_package(Qt5
...
CMAKE_AUTOSTATICPLUGINS
was replaced by a new upstream implementation and is not needed anymore.
CMAKE_AUTOSTATICPLUGINS
被新的上游实现取代,不再需要。
回答by kevin higgins
I just compiled an application statically (Debug) with QT Plugins(5.9), with VS (2015) (Win).
我刚刚使用 QT Plugins(5.9) 和 VS (2015) (Win) 静态编译了一个应用程序(调试)。
a) Add to your code.
a) 添加到您的代码中。
#include <QtPlugin>
Q_IMPORT_PLUGIN (QWindowsIntegrationPlugin);
b) Add the following to the link paths
b) 将以下内容添加到链接路径中
.9.0_x86_static_install\lib
.9.0_x86_static_install\bin
.9.0_x86_static_install\plugins
.9.0_x86_static_install\plugins\platforms
.9.0_x86_static_install\plugins\imageformats
c) Add the list of QT static libraries and internal VS libraries to your link list.
c) 将 QT 静态库和内部 VS 库的列表添加到您的链接列表中。
version.lib
imm32.lib
shlwapi.lib
rpcrt4.lib
Ws2_32.lib
Mpr.lib
Netapi32.lib
Rpcrt4.lib
Iphlpapi.lib
winmm.lib
gdi32.lib
advapi32.lib
msimg32.lib
UxTheme.lib
translatord.lib
preprocessord.lib
d3d9.lib
dxguid.lib
libEGLd.lib
libGLESv2d.lib
iphlpapi.lib
psapi.lib
ws2_32.lib
Dwmapi.lib
Qt5CoreD.lib
Qt5Guid.lib
Qt5Xmld.lib
Qt5Widgetsd.lib
Qt5Networkd.lib
Qt5Winextrasd.lib
Qt5PlatformCompositorSupportd.lib
qicod.lib
qtmaind.lib
qtlibpngd.lib
qtharfbuzzd.lib
qtpcre2d.lib
qwindowsd.lib
Qt5FontDatabaseSupportd.lib
Qt5ThemeSupportd.lib
Qt5EventDispatcherSupportd.lib
Qt5AccessibilitySupportd.lib
qtfreetyped.lib
Kevin Higgins
凯文·希金斯
回答by KingKong
I recommend you to use linuxdeployqtthis tool. It can help solve most of the dependency problems. And I use it to package my qt application successfully.
我推荐你使用linuxdeployqt这个工具。它可以帮助解决大部分依赖问题。我用它成功地打包了我的 qt 应用程序。