C++ 如何为 Qt 应用程序创建可执行文件?

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

How to create executable file for a Qt Application?

c++windowsvisual-studioqt

提问by Paulo Facó

I've been searching on the internet for some useful and clear information about this, it's annoying that such a trivial thing is so hard to do.

我一直在互联网上搜索有关此的一些有用且清晰的信息,如此琐碎的事情很难做到令人讨厌。

What I need is to create an executable file for my Qt project, in order to run it in other computers. My project runs nicely in the Qt Creator, but when I try to run the executable created in the release folder I get errors about some dll missing, and though I have put all of them in the same directory of my .exe I now got some errors about entrypoints in procedures inside the dll (QtCore4.dll).

我需要为我的 Qt 项目创建一个可执行文件,以便在其他计算机上运行它。我的项目在 Qt Creator 中运行良好,但是当我尝试运行在发布文件夹中创建的可执行文件时,我收到有关缺少某些 dll 的错误,尽管我已将它们全部放在我的 .exe 的同一目录中,但我现在得到了一些关于 dll (QtCore4.dll) 内过程入口点的错误。

The solution looks like to be a static linking, I've found many information about it, but none of them really helped me. Here is what I have tried so far:

该解决方案看起来像是一个静态链接,我找到了很多关于它的信息,但没有一个真正帮助我。这是我迄今为止尝试过的:

  • Using the executable created in the release folder as I said above.
  • Static Linking, following this tutorial (and many others, all almost the same thing): http://doc.qt.io/qt-4.8/deployment-windows.htmlbut I get the following error during the "configure -static": NMAKE : fatal error U1077: '"c:\Arquivos de programas\Microsoft Visual Studio 9. 0\VC\BIN\cl.EXE"' : return code '0x2' Stop. Building qmake failed, return code 2
  • 使用我上面所说的在发布文件夹中创建的可执行文件。
  • 静态链接,遵循本教程(以及许多其他教程,几乎都是一样的):http: //doc.qt.io/qt-4.8/deployment-windows.html但我在“configure -static”期间收到以下错误: NMAKE : 致命错误 U1077: '"c:\Arquivos de programas\Microsoft Visual Studio 9. 0\VC\BIN\cl.EXE"' : 返回代码 '0x2' 停止。构建 qmake 失败,返回代码 2

For that I couldn't find any solution, so here I am, just trying to create an executable file for my Qt project, hope you guys can give me some light here.

为此我找不到任何解决方案,所以我在这里,只是尝试为我的 Qt 项目创建一个可执行文件,希望你们能在这里给我一些启发。

采纳答案by Max Lambertini

Basically you have to look for mingw subfolder deep into qt tree, where qt utilities reside, and copy needed dll's.

基本上,您必须在 qt 树的深处寻找 mingw 子文件夹,qt 实用程序所在的位置,并复制所需的 dll。

These are the steps I follow, based upon QT 4.7.4, for packaging the application with correct shared libraries.

这些是我遵循的步骤,基于 QT 4.7.4,用于使用正确的共享库打包应用程序。

  1. Let's say you've installed QT under c:\qtsdk
  2. Open your project, and compile it in release mode.
  3. Go to this directory: C:\QtSDK\Desktop\Qt\4.7.4\mingw\bin -- it contains all shared libraries. Debug libraries end with a "d" -- frex, QtCore.dll is release version, while QtCoreD.dll is debug version
  4. Copy at leastthese files into your release directory (where your .exe lies)

    • mingwm10.dll
    • libgcc_s_dw2-1.dll
    • QtCore4.dll
    • QtGui4.dll

    I just built, tested and deployed a dummy project this way.

  1. 假设您已经在 c:\qtsdk 下安装了 QT
  2. 打开您的项目,并在发布模式下编译它。
  3. 转到这个目录:C:\QtSDK\Desktop\Qt\4.7.4\mingw\bin——它包含所有共享库。调试库以“d”结尾——frex,QtCore.dll 是发布版本,而 QtCoreD.dll 是调试版本
  4. 复制至少这些文件到你的发布目录(其中您的.exe谎言)

    • mingwm10.dll
    • libgcc_s_dw2-1.dll
    • QtCore4.dll
    • QtGui4.dll

    我刚刚以这种方式构建、测试和部署了一个虚拟项目。

回答by gakwaya

Here is what I did and I got it to work after some time;after making sure that your program builds inside Qtcreator,go to the directory that contains your exe and double click on it.(I'm using windows and don't know if it is the same on other platforms).A window should popup telling you which .dll is missing.Go to the directory where Qt is installed and type the exact name in the search bar(top-right in Windows 7) and the dll should show up in search results.Right click and choose open file location.Copy the file where your exe is located .Double click on the exe again.If another .dll is missing repeat the same thing until none is missing.In the end your program should run. Hope this helps.

这是我所做的,一段时间后我让它开始工作;在确保您的程序在 Qtcreator 中构建后,转到包含您的 exe 的目录并双击它。(我正在使用 Windows 并且不知道如果它在其他平台上相同。应该弹出一个窗口,告诉您缺少哪个 .dll。转到安装 Qt 的目录并在搜索栏中键入确切名称(Windows 7 中的右上角)和 dll应显示在搜索结果中。右键单击并选择打开文件位置。复制您的 exe 所在的文件。再次双击该 exe。如果另一个 .dll 丢失,请重复相同的操作,直到没有丢失为止。最后您的程序应该运行。希望这可以帮助。

回答by vizZ

So you downloaded Qt SDK and you want to link Qt libraries statically into you application, right?

所以您下载了 Qt SDK 并且想要将 Qt 库静态链接到您的应用程序中,对吗?

First of all, if you link statically, you don't need any Qt*.dll to be present anywhere because it is built into the application. So that alone means that your application is built using dynamic linking.

首先,如果您静态链接,您不需要任何 Qt*.dll 出现在任何地方,因为它内置于应用程序中。因此,仅此一项就意味着您的应用程序是使用动态链接构建的。

From my experience, that safest way to get it all working is:

根据我的经验,让一切正常工作的最安全方法是:

Download Qt sources, build and install them with nmake ( using Visual Studio Command Prompt from Menu Start -> Application -> Visual Studio 2008 ). Just remember that the size of the statically linked application is dependent on the size of the *.libs and these are dependent on the pre-build configuration, for example, you can minimize the size of you application by turning off some features ( this is an example config for MacOSX, just watch out for the ones specific for Windows and add proper plaftorm - see configure.exe --helpfor details ):

下载 Qt 源代码,使用 nmake 构建和安装它们(从菜单开始 -> 应用程序 -> Visual Studio 2008 使用 Visual Studio 命令提示符)。请记住,静态链接应用程序的大小取决于 *.libs 的大小,而这些取决于预构建配置,例如,您可以通过关闭某些功能来最小化应用程序的大小(这是MacOSX 的示例配置,只需注意特定于 Windows 的配置并添加适当的平台 -configure.exe --help有关详细信息,请参阅):

./configure -arch x86 -platform macx-g++ --universal -cocoa -debug-and-release -opensource -static -fast -no-accessibility -no-sql-sqlite -no-qt3support -no-opengl -no-openvg -qt-zlib -no-gif -no-libmng -qt-libmng -no-libtiff -qt-libjpeg -no-3dnow -sse -no-sse2 -no-openssl -no-dbus -no-phonon -no-multimedia -no-audio-backend -webkit -no-script -no-scripttools -no-declarative -nomake demos -nomake examples -no-exceptions -no-accessibility -confirm-license

Install, setup and use Visual Studio Add-In for you Qt project (here).

为您的 Qt 项目安装、设置和使用 Visual Studio 插件(此处)。

Good Luck! :)

祝你好运!:)

回答by hossein

Do these steps:

执行以下步骤:

1- Open qt command-line window.

1- 打开 qt 命令行窗口。

Qt command-line window

Qt 命令行窗口

2- Go to the directory which .exe file exists (using cd command).

2- 转到 .exe 文件所在的目录(使用 cd 命令)。

3- Run "windeployqt.exe --quick ." command

3- 运行“windeployqt.exe --quick”。命令

4- Run your exe file and enjoy it!

4-运行您的exe文件并享受它!

This video is great

这个视频很棒