C++ 如何在 Windows 或 Linux、32 或 64 位、静态或动态的 Visual Studio 或 g++ 下编译 Qt 5

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

How to compile Qt 5 under Windows or Linux, 32 or 64 bit, static or dynamic on Visual Studio or g++

c++visual-studio-2010qtcompilationvisual-studio-2012

提问by The Quantum Physicist

Just a post to help those guys trying to do that, since I don't have a blog.

只是一个帖子来帮助那些试图做到这一点的人,因为我没有博客。

This works for linux too. Feel free to edit it and improve it.

这也适用于 linux。随意编辑它并改进它。

回答by The Quantum Physicist

Note: There's also another article I wrote to compile from GIT source here with an automated scripton Windows. You may modify it for Linux as the difference is shown in this post.

注意:我还写了另一篇文章,用Windows上的自动化脚本从 GIT 源代码编译。您可以针对 Linux 修改它,因为本文中显示了差异。

This article is continuously being updated. If this helps you, please give it a "thumbs up" so that I could know that it's helping people and not being useless.

本文持续更新中。如果这对您有帮助,请给它“竖起大拇指”,以便我知道它是在帮助人们而不是无用。

If you have any comments or you found typos, please let me know so that I can fix them.

如果您有任何意见或发现错别字,请告诉我,以便我可以更正。

First thing, it doesn't matter whether you want to compile 32 or 64 bit version. The only difference is the command prompt shortcut that you have to choose from Visual Studio, which will initialize different environment variables.

首先,您要编译 32 位还是 64 位版本并不重要。唯一的区别是您必须从 Visual Studio 中选择的命令提示符快捷方式,它将初始化不同的环境变量。



Let's begin with this:

让我们从这个开始:

  1. Download and install Perl: Download link

  2. Download and install Python: Download link

  3. Download and install Windows SDK(probably not necessary, but recommended) I use Windows 8, so this is the version I used: Download link,Otherwise find the proper version for your Windows.

  4. Download and install DirectX SDK(probably necessary if you wanna compile with OpenGL) Download link

  5. Download and extract jomto some folder (not needed for linux) (jomis a tool for compiling stuff with VS in parallel, there's a way to do this with nmake, but I'm not familiar with it) Download link

  6. Download Qt Opensource, and extract it, say to C:\Qt\Qt5.6, so now the folder qtbasecan be found in C:\Qt\Qt5.6\qtbase.

  7. Only for Windows: DO YOU REALLY WANT IT TOTALLY STATIC?

    Usually, even if you choose the compilation to be static, the compiler itself will still not merge its libraries statically. If you want your compiled source to be fully static with respect to the compiler (Visual Studio), you have to do this tweak in the QMAKE files of Qt.

    Go to the file (starting from your Qt source directory), for versions higher than 2012, just use the right version everywhere (such as win32-msvc2015):

    • a. For VS2012:qtbase\mkspecs\win32-msvc2012\qmake.conf

    • b. For VS2010:qtbase\mkspecs\win32-msvc2010\qmake.conf

    • c. For Qt 5.5.0 and later(with any VS version): qtbase\mkspecs\common\msvc-desktop.conf

    and edit the following lines

     QMAKE_CFLAGS_RELEASE    = -O2 -MD
     QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MD -Zi
     QMAKE_CFLAGS_DEBUG      = -Zi -MDd
    

    to

     QMAKE_CFLAGS_RELEASE    = -O2 -MT
     QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MT -Zi
     QMAKE_CFLAGS_DEBUG      = -Zi -MTd
    
  1. 下载并安装 Perl:下载链接

  2. 下载并安装 Python:下载链接

  3. 下载并安装Windows SDK(可能不是必需的,但推荐)我使用 Windows 8,所以这是我使用的版本:下载链接,否则为您的 Windows 找到合适的版本。

  4. 下载并安装DirectX SDK(如果你想用OpenGL编译,可能需要) 下载链接

  5. jom下载并解压到某个文件夹(Linux 不需要)(jom是一个用于与 VS 并行编译东西的工具,有一种方法可以使用nmake来做到这一点,但我不熟悉它)下载链接

  6. 下载Qt Opensource并解压,比如 to C:\Qt\Qt5.6,所以现在qtbase可以在C:\Qt\Qt5.6\qtbase.

  7. 仅适用于 Windows:您真的希望它完全静态吗?

    通常,即使您选择静态编译,编译器本身仍然不会静态合并其库。如果您希望编译源相对于编译器 (Visual Studio) 完全静态,则必须在 Qt 的 QMAKE 文件中进行此调整。

    转到文件(从你的Qt源目录开始),对于2012以上的版本,只需在任何地方使用正确的版本(例如win32-msvc2015)

    • 一种。对于 VS2012:qtbase\mkspecs\win32-msvc2012\qmake.conf

    • 对于 VS2010:qtbase\mkspecs\win32-msvc2010\qmake.conf

    • C。对于 Qt 5.5.0 及更高版本(任何 VS 版本):qtbase\mkspecs\common\msvc-desktop.conf

    并编辑以下几行

     QMAKE_CFLAGS_RELEASE    = -O2 -MD
     QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MD -Zi
     QMAKE_CFLAGS_DEBUG      = -Zi -MDd
    

     QMAKE_CFLAGS_RELEASE    = -O2 -MT
     QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MT -Zi
     QMAKE_CFLAGS_DEBUG      = -Zi -MTd
    

Note: Qt 5.6+ have a configure parameter -static-runtimethat will do this for you. You may not need to do this manually anymore for new versions of Qt.

注意:Qt 5.6+ 有一个配置参数-static-runtime可以为你做这件事。对于新版本的 Qt,您可能不再需要手动执行此操作。

  1. Start the terminal in linux, or in Windows, start the terminals of Visual Studio (which have the correct environment variables set, or alternatively use vcvarsall.bat). To start the command prompt and let it do this automatically for you, go to Start, All Programs:

    For Windows versions prior to 8:Find the Microsoft Visual Studio 201x folder, and launch the command prompt (either x86 for 32 bit or x64 for 64 bit).

    For Windows 8:go to Start, type "cmd" and all versions available for command prompt will show up. Choose the Visual Studio version appropriate (x86 for 32 bit or x64 for 64 bit).

  1. 在 linux 或 Windows 中启动终端,启动 Visual Studio 的终端(设置了正确的环境变量,或者使用vcvarsall.bat)。要启动命令提示符并让它自动为您执行此操作,请转到“开始”、“所有程序”:

    对于 8 之前的 Windows 版本:找到 Microsoft Visual Studio 201x 文件夹,然后启动命令提示符(x86 用于 32 位或 x64 用于 64 位)。

    对于 Windows 8:转到“开始”,键入“cmd”,然后将显示所有可用于命令提示符的版本。选择合适的 Visual Studio 版本(x86 用于 32 位或 x64 用于 64 位)。

Following is a screenshot of how it may look like. Always tend to select "Native" if it exists.

以下是它的外观的屏幕截图。如果存在,总是倾向于选择“本机”。

enter image description here

在此处输入图片说明

9.

9.

  • For VS2012:Execute the following commands for VS2012

     set QMAKESPEC=win32-msvc2012
     set QTDIR=C:\Qt\Qt5.7\qtbase
     set PATH=C:\Qt\Qt5.7\qtbase\bin;%PATH%
    
  • 对于VS2012:对VS2012执行以下命令

     set QMAKESPEC=win32-msvc2012
     set QTDIR=C:\Qt\Qt5.7\qtbase
     set PATH=C:\Qt\Qt5.7\qtbase\bin;%PATH%
    

Note: Setting QMAKESPECenvironment variable is considered wrong for Qt versions 5.8+. Don't do it for the new versions.

注意:QMAKESPEC对于 Qt 5.8+ 版本,设置环境变量被认为是错误的。不要为新版本这样做。

For dynamic linking (needs 8 GBs)

用于动态链接(需要 8 GB)

configure -debug-and-release -opensource -platform win32-msvc2012 -opengl desktop

For dynamic linking with no examples (needs 2 GB)

对于没有示例的动态链接(需要 2 GB)

configure -debug-and-release -opensource -platform win32-msvc2012 -opengl desktop -nomake examples -nomake tests

 Now the last command depends on what you want to compile. Just type configure -help and see what the available command-line parameters are.

For static linking (needs 70 GBs, yes it's crazy, it's more reasonable not to make the examples and demos).

对于静态链接(需要 70 GB,是的,这很疯狂,不制作示例和演示更合理)。

configure -debug-and-release -opensource -platform win32-msvc2012 -opengl desktop -static

For static linking with no examples (needs 4 GBs, makes more sense).

对于没有示例的静态链接(需要 4 GB,更有意义)。

configure -debug-and-release -opensource -platform win32-msvc2012 -opengl desktop -static -nomake examples -nomake tests

Now this will take a minute or two, then use jom as follows (assuming it's extracted in C:\Qt\jom):

现在这将需要一两分钟,然后按如下方式使用 jom(假设它是在 C:\Qt\jom 中提取的):

C:\Qt\jom\jom.exe -j 50

50 represents the number of cores you want to use. I use 50 because I have 8 threads and using only 8 will not occupy all cores completely, so more is better, but don't get too greedy as it could make your system not responsive.

50 表示您要使用的核心数。我使用 50 是因为我有 8 个线程并且只使用 8 个不会完全占用所有内核,所以更多更好,但不要太贪婪,因为它可能会使您的系统没有响应。

  • For VS2010:Execute the following commands for VS2010

    set QMAKESPEC=win32-msvc2010
    set QTDIR=C:\Qt\Qt5.7\qtbase
    set PATH=C:\Qt\Qt5.7\qtbase\bin;%PATH%
    
  • 对于VS2010:对VS2010执行以下命令

    set QMAKESPEC=win32-msvc2010
    set QTDIR=C:\Qt\Qt5.7\qtbase
    set PATH=C:\Qt\Qt5.7\qtbase\bin;%PATH%
    

Note: Setting QMAKESPECenvironment variable is considered wrong for Qt versions 5.8+. Don't do it for the new versions. For dynamic linking (needs 8 GBs)

注意:QMAKESPEC对于 Qt 5.8+ 版本,设置环境变量被认为是错误的。不要为新版本这样做。用于动态链接(需要 8 GB)

configure -debug-and-release -opensource -platform win32-msvc2010 -opengl desktop

For dynamic linking with no examples (needs 2 GB)

对于没有示例的动态链接(需要 2 GB)

configure -debug-and-release -opensource -platform win32-msvc2010 -opengl desktop -nomake examples -nomake tests

The last command depends on what you want to compile. Just type configure -help and see what the available command-line parameters are. For static linking (needs 70 GBs, yes it's crazy, it's more reasonable not to make the examples and demos).

最后一个命令取决于您要编译的内容。只需键入 configure -help 并查看可用的命令行参数是什么。对于静态链接(需要 70 GB,是的,这很疯狂,不制作示例和演示更合理)。

configure -debug-and-release -opensource -platform win32-msvc2010 -opengl desktop -static

For static linking with no examples (needs 4 GBs, makes more sense).

对于没有示例的静态链接(需要 4 GB,更有意义)。

configure -debug-and-release -opensource -platform win32-msvc2010 -opengl desktop -static -nomake examples -nomake tests

Now this will take a minute or two, then use jom as follows (assuming it's extracted in C:\Qt\jom):

现在这将需要一两分钟,然后按如下方式使用 jom(假设它是在 C:\Qt\jom 中提取的):

C:\Qt\jom\jom.exe -j 50

50 represents the number of cores you want to use. I use 50 because I have 8 threads and using only 8 will not occupy all cores completely, so more is better, but don't get too greedy as it could make your system not responsive.

50 表示您要使用的核心数。我使用 50 是因为我有 8 个线程并且只使用 8 个不会完全占用所有内核,所以更多更好,但不要太贪婪,因为它可能会使您的系统没有响应。

  • For linux:
  • 对于Linux:

There's one small difference for Linux over Windows. It's recommended in linux to installafter compiling. Honestly this is the only way it works for me without problems.

Linux 与 Windows 之间存在一个小差异。建议在linux下编译后安装。老实说,这是对我没有问题的唯一方法。

Execute the following commands for Linux. Don't forget to replace the paths with the correct paths of your Qt source

对 Linux 执行以下命令。不要忘记用 Qt 源的正确路径替换路径

    export QMAKESPEC=linux-g++
    export QTDIR=/home/username/Qt5.7/qtbase
    export PATH=/home/username/Qt5.7/qtbase/bin:$PATH

Note: Setting QMAKESPECenvironment variable is considered wrong for Qt versions 5.8+. Don't do it for the new versions.

注意:QMAKESPEC对于 Qt 5.8+ 版本,设置环境变量被认为是错误的。不要为新版本这样做。

Let's say you want to install the compiled source to the directory /home/username/Qt5.7-install. In this case, add the following to anyof the configure commands below:

假设您要将编译后的源代码安装到目录/home/username/Qt5.7-install. 在这种情况下,请将以下内容添加到以下一配置命令中:

-prefix /home/username/Qt5.7-install

Warning: DO NOT install to the same source directory. That's plain wrong!

警告:不要安装到相同的源目录。那是完全错误的!

If -prefixis not set, the default path will be chosen, which is /usr/local/I guess. I don't like to install anything using root. I always prefer installing in my user folder, so that reversibility and upgrades are not a problem.

如果-prefix未设置,将选择默认路径,这是/usr/local/我猜的。我不喜欢使用 root 安装任何东西。我总是喜欢在我的用户文件夹中安装,这样可逆性和升级就不是问题。

The following are different possible configurecommands depending on what you want to do.

以下是不同的可能configure命令,具体取决于您要执行的操作。

For dynamic linking (needs 8 GBs)

用于动态链接(需要 8 GB)

./configure -debug-and-release -opensource -platform linux-g++ -opengl desktop

For dynamic linking with no examples (needs 2 GB)

对于没有示例的动态链接(需要 2 GB)

./configure -debug-and-release -opensource -platform linux-g++ -opengl desktop -nomake examples -nomake tests

Now the last command depends on what you want to compile. Just type ./configure -helpand see what the available command-line parameters are.

现在最后一个命令取决于您要编译的内容。只需键入 ./configure -help并查看可用的命令行参数是什么。

For static linking (needs 70 GBs, yes it's crazy, it's more reasonable not to make the examples and tests).

对于静态链接(需要 70 GB,是的,这很疯狂,不制作示例和测试更合理)。

./configure -debug-and-release -opensource -platform linux-g++ -opengl desktop -static

For static linking with no examples (needs 4 GBs, makes more sense).

对于没有示例的静态链接(需要 4 GB,更有意义)。

./configure -debug-and-release -opensource -platform linux-g++ -opengl desktop -static -nomake examples -nomake tests

After making is done, run make command

制作完成后,运行make命令

make -j 50

50 represents the number of cores you want to use. I use 50 because I have 8 threads and using only 8 will not occupy all cores completely, so more is better, but don't get too greedy as it could make your system not responsive.

50 表示您要使用的核心数。我使用 50 是因为我有 8 个线程并且只使用 8 个不会完全占用所有内核,所以更多更好,但不要太贪婪,因为它可能会使您的系统没有响应。

  1. Wait 2+ hours till the compilation is complete.

  2. Clean up! You can save a lot of space using this command for Windows: C:\Qt\jom\jom.exe cleanAnd this command for linux: make clean

  1. 等待 2 个多小时,直到编译完成。

  2. 清理!在 Windows 上使用此命令可以节省大量空间:C:\Qt\jom\jom.exe clean而对于 linux使用此命令:make clean

You can reduce the size of your compiled folder from 8 GB to 2.5 GB (for dynamic linking) and from 70 GB to 35 GB (for static linking).

您可以将编译文件夹的大小从 8 GB 减少到 2.5 GB(用于动态链接)和从 70 GB 减少到 35 GB(用于静态链接)。



In order to use this compiled version in Qt Creator:

为了在 Qt Creator 中使用这个编译版本:

  1. Start Qt Creator
  2. Go to Tools, Options
  3. Select Build and Run from the list on the left.
  4. Go to "Qt Versions" tab
  5. Click on "Add" and select qmake from the folder where your bin in qtbase is, so from above:

    C:\Qt\Qt5.7\qtbase\bin\qmake.exe

  1. 启动 Qt Creator
  2. 转到工具,选项
  3. 从左侧的列表中选择 Build and Run。
  4. 转到“Qt 版本”选项卡
  5. 单击“添加”并从 qtbase 中的 bin 所在的文件夹中选择 qmake,从上面开始:

    C:\Qt\Qt5.7\qtbase\bin\qmake.exe

(or for Linux choose the path where you installed the compiled Qt source, which is equivalent to /home/username/Qt5.7-install/qtbase/bin/qmakein this tutorial)

(或者对于 Linux 选择你安装编译好的 Qt 源的路径,这相当于/home/username/Qt5.7-install/qtbase/bin/qmake在本教程中)

  1. Click "Apply"
  2. Go to "Kits" tab
  3. Click "Add"
  4. Give it a name, choose the appropriate compiler (FOR VISUAL STUDIO EXPRESS DO NOT SELECT amd64 FOR 64-BIT , IT WON'T WORK, CHOOSE x86_amd64 INSTEAD)
  5. Click OK.
  1. 点击“申请”
  2. 转到“套件”选项卡
  3. 点击“添加”
  4. 给它一个名字,选择合适的编译器(对于 VISUAL STUDIO EXPRESS DO NOT SELECT amd64 FOR 64-BIT ,它不会工作,选择 x86_amd64 INSTEAD)
  5. 单击确定。

Now just open a project and you'll find it asking you to choose the kit you added.

现在只需打开一个项目,您就会发现它要求您选择您添加的套件。

Enjoy :)

享受 :)