C++ 如何为 Visual Studio 2010 构建 Qt

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

How to build Qt for Visual Studio 2010

c++visual-studiovisual-studio-2010qtbuild

提问by hyperknot

I struggled finding a how-to which provides a stable solution for using Qt with Visual Studio 2010, so after collecting all the bits of information and some trial and error, I would like to write my solution into a guide.

我一直在努力寻找一个如何为在 Visual Studio 2010 中使用 Qt 提供稳定解决方案的方法,因此在收集了所有信息和一些反复试验之后,我想将我的解决方案写入指南。

The problem, or why is it not possible to use prebuilt binaries?

问题,或者为什么不能使用预构建的二进制文件?

It seems that using binaries built for Visual Studio 2008 might work in some special cases, but I found them not to work. In my case they compiled OK, but they produce runtime errors, like this:

似乎使用为 Visual Studio 2008 构建的二进制文件可能在某些特殊情况下工作,但我发现它们不起作用。在我的情况下,它们编译正常,但它们会产生运行时错误,如下所示:

problem2

问题2

or when started from Visual Studio 2010:

或从 Visual Studio 2010 启动时:

problem

问题

Update: I found a blog post analysing why does it work for some people, while it does not for others. In one word, it depends on whether you have Visual Studio 2008 installed on the same machine, or not. http://blog.paulnettleship.com/2010/11/11/troubleshooting-visual-studio-2010-and-qt-4-7-integration/

更新:我发现了一篇博客文章,分析了为什么它对某些人有效,而对其他人无效。一句话,这取决于你是否在同一台机器上安装了 Visual Studio 2008。 http://blog.paulnettleship.com/2010/11/11/troubleshooting-visual-studio-2010-and-qt-4-7-integration/

The most important thing (that I stupidly didn't realize) was the fact that you CANNOT use the Visual Studio 2008 compiled libraries and dll's (available on the Qt webpage) if you don't have Visual Studio 2008 installed. The reason is because the Qt SDK you download is a debug build which is dependant on the VC9.0 DebugCRT, meaning it needs the Visual C++ 2008 Debug Runtime installed, which is NOT available as a redistributable installer. The only way to install the DebugCRT is to install the entirety of Visual Studio 2008.

最重要的事情(我愚蠢地没有意识到)是,如果您没有安装 Visual Studio 2008,则不能使用 Visual Studio 2008 编译库和 dll(可在 Qt 网页上找到)。原因是您下载的 Qt SDK 是一个依赖于 VC9.0 DebugCRT 的调试版本,这意味着它需要安装 Visual C++ 2008 调试运行时,它不能作为可再发行安装程序使用。安装 DebugCRT 的唯一方法是安装整个 Visual Studio 2008。

回答by hyperknot

First of all, it's very important to understand that for using Qt with Visual Studio 2010, it's not possible to use the pre-built binaries which were made for Visual Studio 2008, but you have to compile it from source.

首先,了解在 Visual Studio 2010 中使用 Qt非常重要,不可能使用为 Visual Studio 2008 制作的预构建二进制文件,但您必须从源代码编译它。

Downloading Qt

下载 Qt

On https://www.qt.io/download/

https://www.qt.io/download/

Update 2017:the latest Qt 4.x branch (Qt 4.8.6) has 2 pre-built packages, which are now in the archive section:

2017 年更新:最新的 Qt 4.x 分支(Qt 4.8.6)有 2 个预构建的包,它们现在位于存档部分:

  1. http://download.qt.io/archive/qt/4.8/4.8.6/qt-opensource-windows-x86-vs2010-4.8.6.exe
  2. http://download.qt.io/archive/qt/4.8/4.8.6/qt-opensource-windows-x86-vs2008-4.8.6.exe
  1. http://download.qt.io/archive/qt/4.8/4.8.6/qt-opensource-windows-x86-vs2010-4.8.6.exe
  2. http://download.qt.io/archive/qt/4.8/4.8.6/qt-opensource-windows-x86-vs2008-4.8.6.exe

You should not download Qt by clicking "Qt libraries 4.8.6 for Windows (Visual Studio 2008, 218 MB)", but by clicking on the "zip"link above it.

您不应通过单击“Qt libraries 4.8.6 for Windows (Visual Studio 2008, 218 MB)”来下载 Qt ,而应单击其上方的“zip”链接。

link-selection

链接选择

On that link, you get a big zip file like "qt-everywhere-opensource-src-4.8.6.zip". Unzip this into a folder and make its path something nice and small, for example "E:\Qt"

在那个链接上,你会得到一个很大的 zip 文件,比如"qt-everywhere-opensource-src-4.8.6.zip"。将其解压缩到一个文件夹中,并使其路径变得更小,例如“E:\Qt”

Visual Studio Command Prompt

Visual Studio 命令提示符

Now that we have the sources, we need to build the binaries. To do it, open the Microsoft Visual Studio 2010\Visual Studio Tools\Visual Studio Command Prompt (2010)link from your start menu, or even pin it to the taskbar (a good idea). This is a special command prompt which has all the variables set for building with Visual Studio 2010 tools.

现在我们有了源代码,我们需要构建二进制文件。为此,请Microsoft Visual Studio 2010\Visual Studio Tools\Visual Studio Command Prompt (2010)从开始菜单中打开链接,甚至将其固定到任务栏(一个好主意)。这是一个特殊的命令提示符,其中设置了用于使用 Visual Studio 2010 工具进行构建的所有变量。

Once within the command prompt, navigate to your extracted Qt folder using old-school DOS way, which means you have to change drive letter by E:, enter directories by cd Qtand list dir contents by dir. You can use the tab key for helping you with the directory names. When you have arrived at the correct directory, a dircommand should return something like this.

进入命令提示符后,使用老式 DOS 方式导航到提取的 Qt 文件夹,这意味着您必须更改驱动器号E:,输入目录cd Qt并列出目录内容dir。您可以使用 Tab 键来帮助您设置目录名称。当您到达正确的目录时,dir命令应该返回如下内容。

command line

命令行

Building Qt

构建 Qt

Now it's time for configure and build. For configuring a minimalist Qt, I'm using the following flags with configure.exe. Just copy and paste it into the command line. Look in the Qt reference manualfor what flag to use or not to use.

现在是配置和构建的时候了。为了配置极简的 Qt,我将以下标志与configure.exe. 只需将其复制并粘贴到命令行中即可。在Qt 参考手册中查看要使用或不使用的标志。

configure.exe -release -no-webkit -no-phonon -no-phonon-backend -no-script -no-scripttools -no-qt3support -no-multimedia -no-ltcg

Once configure.exehas finished (it was 10 minutes for me), you'll need to start the build process. It will take about 20-30 minutes with the above flags. To start it, just type:

一旦configure.exe完成(对我来说是10分钟),你就需要开始构建过程。使用上述标志大约需要 20-30 分钟。要启动它,只需键入:

nmake

Setting environment variables

设置环境变量

Basically, we are done. All you need to do is to set your environment variables (QTDIRand PATH), which tell programs where to find Qt. If you are on Windows 7, you can use the following command to set QTDIRto your installation dir.

基本上,我们已经完成了。你需要做的就是设置你的环境变量(QTDIRPATH),它告诉程序在哪里可以找到 Qt。如果您使用的是 Windows 7,则可以使用以下命令设置QTDIR为您的安装目录。

setx QTDIR e:\Qt

For setting the PATH, I strongly recommend using Path Editor. Within Path Editor

对于设置PATH,我强烈建议使用Path Editor。在路径编辑器中

add the directory of Qt\bin to your PATH

(it doesn't matter if it's in system path or user path)

(无论是在系统路径还是用户路径中都没有关系)

If you prefer to use Control Panel\System\Environment Variables, then you can set these there, too.

如果您更喜欢使用Control Panel\System\Environment Variables,那么您也可以在那里设置这些。

Qt Visual Studio Add-in

Qt Visual Studio 插件

Here you go, after a logoff-logon or a restart, all the Qt demo applications should start correctly (I recommend have a look at bin\qtdemo.exe). Now you can download and install the Visual Studio Add-in (qt-vs-addin-1.1.9.exe) from the Qt download page, it will work perfectly.

在这里,在注销登录或重新启动后,所有 Qt 演示应用程序都应该正确启动(我建议查看bin\qtdemo.exe)。现在您可以从 Qt 下载页面下载并安装 Visual Studio Add-in (qt-vs-addin-1.1.9.exe),它将完美运行。

addin

加入

Appendix A: Official Instructions:

附录 A:官方说明:

There is a page at the official wiki at the Qt website called Qt 4.8 Installing Qt for Windows, but I found it lacking important information.

Qt 网站的官方 wiki 上有一个页面,名为Qt 4.8 安装 Qt for Windows,但我发现它缺少重要信息。

References

参考

Qt DevNet forums
Recommended flags for a minimalistic Qt build
Building Qt 4.5 with Visual C++ 2010
How to compile Qt as static
Qt 4.8: Configure options for Qt
Edit the PATH environment variable in
Windows without pain - op111.net

Qt DevNet 论坛
简约 Qt 构建的推荐标志
使用 Visual C++ 2010 构建 Qt 4.5
如何将 Qt 编译为静态
Qt 4.8:配置 Qt 选项

Windows 中轻松编辑 PATH 环境变量- op111.net

回答by parsley72

Qt V4.8.0 contains prebuilt binaries for Visual Studio 2010 so you don't need to do this anymore: http://qt.nokia.com/downloads/windows-cpp-vs2010

Qt V4.8.0 包含 Visual Studio 2010 的预构建二进制文件,因此您无需再执行此操作:http: //qt.nokia.com/downloads/windows-cpp-vs2010