如何在Windows或Linux,32或64位,Visual Studio或g ++上的静态或动态版本下编译Qt 5 [英] How to compile Qt 5 under Windows or Linux, 32 or 64 bit, static or dynamic on Visual Studio or g++
问题描述
因为我没有博客,这只是一篇文章来帮助那些尝试这样做的人。
这也适用于linux。随时对其进行编辑和改进。
注意:还有我写给
9。
-
对于VS2012:对VS2012执行以下命令
设置QMAKESPEC = win32-msvc2012
设置QTDIR = C:\Qt\Qt5.7\qtbase
设置PATH = C:\Qt\Qt5.7\qtbase\bin;%PATH%
注意:对于Qt 5.8及更高版本,设置 QMAKESPEC
环境变量被认为是错误的。
用于动态链接(需要8 GB)
配置-debug-and-release -opensource -platform win32-msvc2012 -opengl桌面
不带示例的动态链接(需要2 GB)
configure -debug-and-release -opensource -platform win32 -msvc2012 -opengl桌面-nomake示例-nomake测试
现在,最后一条命令取决于您要编译的内容。只需键入configure -help并查看可用的命令行参数。
对于静态链接(需要70 GB,是的,这太疯狂了,不制作示例和演示)。
配置-调试和发布-开源-平台win32-msvc2012 -opengl桌面-静态
对于没有示例的静态链接(需要4 GB,更有意义)。
配置-debug-and-release -opensource -platform win32-msvc2012 -opengl桌面-static -nomake示例-nomake测试
现在这将需要一两分钟,然后按如下所示使用jom(假设它提取在C:\Qt\jom中):
C:\Qt\jom\jom.exe -j 50
50表示要使用的内核数。我使用50是因为我有8个线程,而仅使用8个线程不会完全占据所有内核,所以更多更好,但不要太贪心,因为它可能会使您的系统无响应。
-
对于VS2010:为VS2010执行以下命令
set QMAKESPEC = win32-msvc2010
set QTDIR = C:\Qt\Qt5.7\qtbase
set PATH = C:\Qt\Qt5.7\ qtbase\bin;%PATH%
注意:对于Qt 5.8及更高版本,设置 QMAKESPEC
环境变量被认为是错误的。请勿在新版本中使用。
用于动态链接(需要8 GB)
配置-debug-and-release -opensource -platform win32-msvc2010 -opengl桌面
用于动态链接而无示例(需要2 GB)
配置-调试和发布-开源-平台win32-msvc2010 -opengl桌面-nomake示例-nomake测试
最后一条命令取决于您要编译的内容。只需键入configure -help并查看可用的命令行参数。
对于静态链接(需要70 GB,是的,这很疯狂,不进行示例和演示比较合理)。
configure -debug-and-release -opensource -platform win32-msvc2010 -opengl desktop -static
对于没有示例的静态链接(需要4 GB,更有意义)。
configure -debug-and-release -opensource-平台win32-msvc2010 -opengl桌面-静态-nomake示例-nomake测试
现在这将需要一分钟一两个,然后按以下方式使用jom(假设它在C:\Qt\jom中提取):
C:\ \Qt\jom\jom.exe -j 50
50代表所需的内核数使用。我使用50是因为我有8个线程,而仅使用8个线程不会完全占据所有内核,所以更多更好,但不要太贪心,因为它可能会使您的系统无响应。
- 对于linux:
Windows上的Linux。建议在Linux中进行编译后 install 。老实说,这是它对我没有问题的唯一方法。
在Linux上执行以下命令。不要忘记用正确的Qt源路径替换路径
export QMAKESPEC = linux-g ++
导出QTDIR = / home / username / Qt5.7 / qtbase
导出PATH = / home / username / Qt5.7 / qtbase / bin:$ PATH
注意:Qt 5.8+版本认为设置 QMAKESPEC
环境变量是错误的。
假设您要将已编译的源代码安装到目录 / home / username / Qt5中。 7次安装
。在这种情况下,请将以下内容添加到以下配置命令的 any 中:
-prefix / home /username/Qt5.7-install
警告:请勿安装到相同的源目录。
如果未设置前缀
,将选择默认路径,即 / usr / local /
我想。我不喜欢使用root来安装任何东西。我总是喜欢在用户文件夹中安装,这样可逆性和升级就不成问题。
以下是可能的不同 configure
命令,具体取决于您要执行的操作。
动态链接(需要8 GB)
./ configure-调试和发布-opensource-平台linux-g ++ -opengl桌面
用于不带示例的动态链接(需要2 GB)
。/configure -debug-and-发布-opensource-平台linux-g ++ -opengl桌面-nomake示例-nomake测试
现在最后一个命令取决于您要编译的内容。只需键入。/configure -help
,查看可用的命令行参数是什么。
对于静态链接(需要70 GB,是的,这太疯狂了,不进行示例和测试是更合理的选择。)
./ configure -debug-和发布-opensource-平台linux-g ++ -opengl桌面-static
用于静态链接,没有示例(需要4 GB,更有意义)。
。/configure-调试和发布-开源-平台linux-g ++ -opengl桌面-static -nomake示例-nomake测试
完成后,运行make命令
make -j 50
50表示要使用的内核数。我使用50是因为我有8个线程,而仅使用8个线程不会完全占据所有内核,所以更多更好,但不要太贪心,因为它可能会使您的系统无响应。
-
等待2个小时以上,直到编译完成。
-
清理!对于Windows,使用此命令可以节省很多空间:
C:\Qt\jom\jom.exe clean
对于Linux,此命令:清理干净
您可以将已编译文件夹的大小从8 GB减小到2.5 GB(用于动态链接)和70 GB至35 GB(用于静态链接)。
在Qt Creator中使用以下编译版本:
- 启动Qt Creator
- 转到工具,选项
- 从左侧列表中选择生成并运行。
- 转到 Qt版本标签
-
单击添加,然后从
qtbase中的bin所在的文件夹中选择qmake,从上至:
C:\Qt\ \Qt5.7\qtbase\bin\qmake.exe
(对于Linux,请选择路径您安装了编译后的Qt源,在本教程中相当于 /home/username/Qt5.7-install/qtbase/bin/qmake
)
- 单击应用
- 转到工具包选项卡
- 单击添加
- 给它起个名字,选择合适的编译器(对于VISUAL STUDIO
,请不要为64位选择amd64,否则将无法工作,请选择
x86_amd64 INSTEAD) $ b - 单击确定。
现在只需打开一个项目,您会发现它要求您选择添加的工具包。
享受:)
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:
Download and install Perl: Download link
Download and install Python: Download link
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.
Download and install DirectX SDK (probably necessary if you wanna compile with OpenGL) Download link
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
Download Qt Opensource, and extract it, say to
C:\Qt\Qt5.6
, so now the folderqtbase
can be found inC:\Qt\Qt5.6\qtbase
.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
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.
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:\Qt\Qt5.7\qtbase set PATH=C:\Qt\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.
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:\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.
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%
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:\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.
- 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.
Wait 2+ hours till the compilation is complete.
Clean up! You can save a lot of space using this command for Windows:
C:\Qt\jom\jom.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:
- Start Qt Creator
- Go to Tools, Options
- Select Build and Run from the list on the left.
- Go to "Qt Versions" tab
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
(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)
- Click "Apply"
- Go to "Kits" tab
- Click "Add"
- 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)
- 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!