C++ 如何让 Qt 和 Qtcreator 静态链接库而不是动态链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20799161/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:19:22  来源:igfitidea点击:

How to make Qt and Qtcreator link the libraries statically instead of dynamic?

c++qtqt-creator

提问by the_naive

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.

我知道这可能是一个与许多其他问题类似的问题,但是在搜索了很多次但未能得到明确有效的解决方案后,我不得不问这个问题。

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-bit (VS 2010, 570 MB),我已经完成了我的编程,一切都完成了。但现在我想将它作为 .exe 文件分发给我的同事,但这样做并不复杂,也避免分发 dll 文件,我需要使用静态链接构建程序。

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?

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

Thanks.

谢谢。

回答by lpapp

You can use the CONFIGvariable for this with qmake:

您可以在CONFIGqmake 中使用该变量:

CONFIG += static

or

或者

CONFIG += staticlib

However, you will need to make sure that you have all the libraries that you wish to bundle up, available as static.

但是,您需要确保您拥有所有要捆绑的库,以静态方式提供。

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 框架本身。官方安装只设置动态库(.dll 文件),因此您需要自己构建 Qt 来完成此操作。

You could use the following commands to build Qtstatically for your own purpose:

您可以使用以下命令Qt为您自己的目的静态构建:

configure -developer-build -opensource -nomake examples -nomake tests -static
qmake -r
nmake

Note that in general when building third-party Qt softwares like yours, you better invoke qmakewith the following parameter to pass your environment properly:

请注意,通常在构建像您这样的第三方 Qt 软件时,您最好qmake使用以下参数调用以正确传递您的环境:

qmake -r -spec win32-msvc2010 

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.

还请注意,正如 Frank 和 ManuelH 在评论中所写的那样,如果您的应用程序未获得 LGPL 的免费许可或至少与 LGPL 兼容,并且您也未使用 Qt 的商业许可,则不允许静态链接。最好在采取您的方法之前确定这一点。

Once that is done, you can use the LIBSvariable 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以常规方式使用该变量,例如:将静态库的路径与库名称一起传递给它,如下所示:

LIBS += -L/path/to/the/static/library -lstaticlibraryname

Note that the static library name passed to the -lparameter should not contain the static library extension, for instance .libon Windows.

请注意,传递给-l参数的静态库名称不应包含静态库扩展名,例如.lib在 Windows 上。

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 dll 文件放在可执行文件旁边,然后将该文件夹部署为“包”。这对你来说可能是更简单的方法。