如何使Qt和Qtcreator静态链接库而不是动态链接库? [英] How to make Qt and Qtcreator link the libraries statically instead of dynamic?

查看:577
本文介绍了如何使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(用于Windows 32位)(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.

您能否描述我如何使用于Windows 32位(VS 2010,570 MB)的Qt 5.2.0 使用静态链接构建整个程序?

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?

谢谢。

推荐答案

您可以使用 CONFIG 使用qmake的变量:

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 <​​/ code>参数的静态库名称不应包含静态库扩展名,例如 .lib 在Windows上。

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.

这篇关于如何使Qt和Qtcreator静态链接库而不是动态链接库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆