简体   繁体   English

在Windows上编译静态QT应用程序

[英]Compiling a static QT application on Windows

I am new to QT and I am enjoying the experience except for the fact I cannot manage to statically link the QT library to the output binaries. 我是QT的新手,除了我无法设法将QT库静态链接到输出二进制文件外,我还很喜欢这种体验。 When I run the output file outside of the QT directory, I get The program can't start beacuse QtCored4.dll is missing . 当我在QT目录之外运行输出文件时,我得到The program can't start beacuse QtCored4.dll is missing Obviously QT is dynamically linking their libraries and requesting a .dll I do not have. 显然,QT正在动态链接其库并请求一个我没有的.dll。 Is there a way to statically compile QT's libraries into a static binary so none of QT's dlls are required? 有没有一种方法可以将QT的库静态编译为静态二进制文件,因此不需要QT的dll? I ask this because I am already up to 11 .dlls for my project, and I would really like to cut down the amount of files that have to be distributed with my software. 我之所以这样问,是因为我的项目已经有11个.dll,并且我真的很想减少必须随我的软件分发的文件量。 Size is not a problem for me. 尺寸对我来说不是问题。 Thanks. 谢谢。

I have tried adding CONFIG += static to the .pro file, but to no avail. 我尝试将CONFIG += static添加到.pro文件,但无济于事。

First off, you can't statically link Qt unless you've bought a commercial license. 首先,除非您购买了商业许可证,否则不能静态链接Qt。 To do so would put you in violation of the LGPL license under which the non-commercial version is distributed. 这样做会违反发行非商业版本的LGPL许可证。 I feel your pain, I've got many, many DLL's to go with my software. 我感到您很痛苦,我的软件附带了很多很多DLL。

Thankfully, you probably do have the DLL's if you used the installer: you don't need to build from source, that should have been done automatically. 值得庆幸的是,如果您使用安装程序,则可能确实具有DLL:您无需从源代码进行构建,这应该是自动完成的。 You'll find them in Qt\\Version\\bin, where Qt is the directory you installed Qt, and version is the version of Qt you installed. 您可以在Qt \\ Version \\ bin中找到它们,其中Qt是您安装Qt的目录,而version是您安装的Qt的版本。 For example, mine is found in G:\\Libraries\\Qt\\4.7.1\\bin. 例如,可以在G:\\ Libraries \\ Qt \\ 4.7.1 \\ bin中找到我的。

I did, however, have some issues with not having one of the DLL's built - one for working with OpenGL - and performed a rebuild to do so. 但是,我确实有一些问题,因为没有构建其中一个DLL(一个用于OpenGL),并执行了重建操作。 I've also done so when I've switched versions of Visual Studio. 当我切换Visual Studio版本时,我也这样做了。 I think it's handy to be able to do so, it's easy, Open a terminal in the Qt directory, and execute: 我认为这样做很方便,很容易,在Qt目录中打开一个终端,然后执行:

configure.exe -platform XXX'

Where XXX denotes the type of build you want to perform. 其中XXX表示您要执行的构建类型。 Valid options include win32-msvc2005, win32-msvc2008, win32-msvc2010. 有效选项包括win32-msvc2005,win32-msvc2008,win32-msvc2010。 So I use: 所以我用:

configure.exe -platform win32-msvc2010'

Other options are detailed here . 其他选项在这里详细说明。 These instructions apply if you've downloaded the source code, however you might have to add the current directory to the path variable like so: 如果已下载源代码,则适用这些说明,但是您可能必须将当前目录添加到path变量中,如下所示:

set PATH=%cd%\bin;%PATH%

The whole procedure should take about an hour. 整个过程大约需要一个小时。

You need to download the source packet of QT and compile it. 您需要下载QT的源数据包并进行编译。 It takes some time but is not really complicated. 这需要一些时间,但并不十分复杂。

  • Download and unzip QT source 下载并解压缩QT源
  • Start a compiler shell (Visual Studio or mingw) 启动编译器外壳(Visual Studio或mingw)
  • Execute configure in the QT source directory - add a flag for static compile here 在QT源目录中执行配置-在此处添加用于静态编译的标志
  • execute make (Visual Studio nmake) 执行make(Visual Studio nmake)
  • Wait some hours depending on the speed of your machine 请等待几个小时,具体取决于机器的速度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM