如何让 Qt 和 Qtcreator 静态链接库而不是动态链接?
我知道这可能是一个与许多其他问题类似的问题,但在搜索了很多次但未能得到明确有效的解决方案后,我不得不提出这个问题.
I know it might be a question similar to many others, but after searching for many times and failing to get a definitive and effective solution I'm having to ask this question.
我正在使用 Qt 5.2.0 for Windows 32-bit (VS 2010, 570 MB),我已经完成了我的编程,一切都完成了.但现在我想将它作为 .exe 文件分发给我的同事,但这样做不会很复杂,也为了避免分发 dll 文件,我需要使用静态链接构建程序.
I'm using Qt 5.2.0 for Windows 32-bit (VS 2010, 570 MB), and I have already made my programming, and it's all done. But now I want to distribute it as .exe file to my colleagues, but to do so without complication and to avoid having to distribute dll files I need to build the program using static linking.
能否请您描述一下我如何制作 Qt 5.2.0 for Windows 32 位(VS 2010,570 MB) 使用静态链接构建整个程序?
Could you please describe how I can make Qt 5.2.0 for Windows 32-bit (VS 2010, 570 MB) build the whole program using static linking?
谢谢.
推荐答案
你可以在 qmake 中使用 CONFIG
变量:
You can use the CONFIG
variable for this with qmake:
CONFIG += static
或
CONFIG += staticlib
但是,您需要确保您拥有所有要捆绑的库,以静态方式提供.
However, you will need to make sure that you have all the libraries that you wish to bundle up, available as static.
如果您遵守许可,这也包括 Qt 框架本身.官方安装只设置动态库(.dll文件),所以需要自己构建Qt来完成.
This includes the Qt framework itself as well if you comply with the license to do so. The official installation only sets up dynamic libraries (.dll files), so you would need to build Qt on your own to accomplish this.
您可以使用以下命令为您自己的目的静态构建 Qt
:
You could use the following commands to build Qt
statically for your own purpose:
configure -developer-build -opensource -nomake examples -nomake tests -static
qmake -r
nmake
请注意,通常在构建像您这样的第三方 Qt 软件时,最好使用以下参数调用 qmake
以正确传递您的环境:
Note that in general when building third-party Qt softwares like yours, you better invoke qmake
with the following parameter to pass your environment properly:
qmake -r -spec win32-msvc2010
还请注意,正如 Frank 和 ManuelH 在评论中所写的那样,如果您的应用程序未获得 LGPL 的免费许可或至少与 LGPL 兼容,并且您也未使用 Qt 的商业许可,则不允许静态链接.最好在采取您的方法之前确定这一点.
Please also noted that as Frank and ManuelH wrote in the comment, static linkage is not allowed if your application is not free licensed either a LGPL or at least compatible to LGPL, nor do you use commercial license for Qt. It is better to make sure about this before picking up your approach.
完成后,您可以以常规方式使用 LIBS
变量,例如:将静态库的路径与库名称一起传递给它,如下所示:
Once that is done, you can use the LIBS
variable in the regular way, as in: pass the path of your static library to it along with the library name, so something like this:
LIBS += -L/path/to/the/static/library -lstaticlibraryname
请注意,传递给 -l
参数的静态库名称不应包含静态库扩展名,例如 Windows 上的 .lib
.
Note that the static library name passed to the -l
parameter should not contain the static library extension, for instance .lib
on Windows.
作为后备,您始终可以静态链接其他库,并将 Qt dll 文件放在可执行文件旁边,然后将该文件夹部署为包".这对您来说可能是更简单的方法.
As a fallback, you can always link other libraries statically, and put the Qt dll files beside the executable, and you deploy the folder as a "package". That is probably the easier way for you to go.
相关文章