如何在 Windows 或 Linux,32 或 64 位,在 Visual Stud

时间:2023-01-23
本文介绍了如何在 Windows 或 Linux,32 或 64 位,在 Visual Studio 或 g++ 上静态或动态编译 Qt 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是为了帮助那些试图这样做的人的帖子,因为我没有博客.

这也适用于 linux.随意编辑和改进它.

解决方案

注意:我还写了另一篇文章给

9.

  • 对于VS2012:针对VS2012执行以下命令

     设置 QMAKESPEC=win32-msvc2012设置 QTDIR=C:QtQt5.7qtbase设置 PATH=C:QtQt5.7qtbasein;%PATH%

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

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

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

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

configure -debug-and-release -opensource -platform win32-msvc2012 -opengl desktop -nomake examples -nomake tests现在最后一个命令取决于您要编译的内容.只需键入 configure -help 并查看可用的命令行参数是什么.

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

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

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

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

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

C:Qtjomjom.exe -j 50

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

  • 对于VS2010:对于VS2010执行以下命令

    set QMAKESPEC=win32-msvc2010设置 QTDIR=C:QtQt5.7qtbase设置 PATH=C:QtQt5.7qtbasein;%PATH%

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

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

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

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

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

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

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

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

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

C:Qtjomjom.exe -j 50

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

  • 对于 linux:

Linux 与 Windows 有一个小区别.建议在linux下编译后安装.老实说,这是对我来说没有问题的唯一方法.

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

 export QMAKSPEC=linux-g++导出 QTDIR=/home/username/Qt5.7/qtbase导出路径=/home/username/Qt5.7/qtbase/bin:$PATH

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

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

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

警告:请勿安装到同一源目录.这完全是错误的!

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

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

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

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

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

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

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

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

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

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

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

制作完成后,运行make命令

make -j 50

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

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

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

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

<小时>

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

  1. 启动 Qt Creator
  2. 转到工具、选项
  3. 从左侧的列表中选择构建并运行".
  4. 转到Qt 版本"标签
  5. 单击添加"并从 bin 所在的文件夹中选择 qmakeqtbase 是,所以从上面:

    C:QtQt5.7qtbaseinqmake.exe

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

  1. 点击应用"
  2. 转到套件"标签
  3. 点击添加"
  4. 给它一个名字,选择合适的编译器(对于 VISUAL STUDIOEXPRESS DO NOT SELECT amd64 FOR 64-BIT ,它行不通,选择x86_amd64 代替)
  5. 点击确定.

现在只需打开一个项目,您就会发现它要求您选择添加的工具包.

享受:)

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.

解决方案

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

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.


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 jom to some folder (not needed for linux) (jom is 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:QtQt5.6, so now the folder qtbase can be found in C:QtQt5.6qtbase .

  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: qtbasemkspecswin32-msvc2012qmake.conf

    • b. For VS2010: qtbasemkspecswin32-msvc2010qmake.conf

    • c. For Qt 5.5.0 and later (with any VS version): qtbasemkspecscommonmsvc-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
    

Note: Qt 5.6+ have a configure parameter -static-runtime that will do this for you. You may not need to do this manually anymore for new versions of 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).

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

9.

  • For VS2012: Execute the following commands for VS2012

     set QMAKESPEC=win32-msvc2012
     set QTDIR=C:QtQt5.7qtbase
     set PATH=C:QtQt5.7qtbasein;%PATH%
    

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

For dynamic linking (needs 8 GBs)

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

For dynamic linking with no examples (needs 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).

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

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

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:Qtjom):

C:Qtjomjom.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.

  • For VS2010: Execute the following commands for VS2010

    set QMAKESPEC=win32-msvc2010
    set QTDIR=C:QtQt5.7qtbase
    set PATH=C:QtQt5.7qtbasein;%PATH%
    

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

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

For dynamic linking with no examples (needs 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 -debug-and-release -opensource -platform win32-msvc2010 -opengl desktop -static

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

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:Qtjom):

C:Qtjomjom.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.

  • For linux:

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

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

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

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

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 any of the configure commands below:

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

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

If -prefix is 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.

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

For dynamic linking (needs 8 GBs)

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

For dynamic linking with no examples (needs 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 -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 tests).

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

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

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

After making is done, run make command

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.

  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:Qtjomjom.exe clean And this command for 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).


In order to use this compiled version in 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:QtQt5.7qtbaseinqmake.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/qmake in this tutorial)

  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.

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

Enjoy :)

这篇关于如何在 Windows 或 Linux,32 或 64 位,在 Visual Studio 或 g++ 上静态或动态编译 Qt 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Q_OBJECT 宏有什么作用?为什么所有 Qt 对象都需要这 下一篇:Qt:调整包含 QPixmap 的 QLabel 的大小,同时保持其纵

相关文章

最新文章