Building Qt 5.14.0 on Windows 10 with Visual Studio 2019编译qt

08/27 15:26
阅读数 95

0 参考

QT最新版5.14在WINDOWS环境静态编译安装和部署的完整过程 VS 2019-QT STATIC LINK BUILD WINDOWS 32 BIT/64 BIT
MSVC 静态编译 Qt 5
Win7下静态编译QT5.12源码
Building Qt 5 from Git

1 获取源码

试过通过官网的MaintenanceTool,还有git获取的源码,编译时总是有报错,最后试着从清华源获取源码,成了。
https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.15/5.15.2/single/qt-everywhere-src-5.15.2.zip

2 编译工具

源码路径下的README文件中有编译方式的说明,其中列出来Windows下编译所需工具

   Windows:
   --------

     Open a command prompt.
     Ensure that the following tools can be found in the path:
     * Supported compiler (Visual Studio 2012 or later,
        MinGW-builds gcc 4.9 or later)
     * Perl version 5.12 or later   [http://www.activestate.com/activeperl/]
     * Python version 2.7 or later  [http://www.activestate.com/activepython/]
     * Ruby version 1.9.3 or later  [http://rubyinstaller.org/]

python需要python2,python3不行,下面是我的版本

This is perl 5, version 32, subversion 1 (v5.32.1) built for MSWin32-x64-multi-thread
Python 2.7.18
ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x64-mingw32] (strawberry-perl-5.32.1.1-64bit)

如果安装有问题或者环境变量有问题之后configure配置时会报warning

3 修改\qt-everywhere-src-5.15.2\qtbase\mkspecs\common\msvc-desktop.conf

将文件中-MD 全改为 -MT(3处)

4 configure配置

打开x64 Native Tools Command Prompt for VS 2019(打算编译成64位就打开x64,32位打开x86),
cd 到源文件文件夹

configure.bat -prefix S:\Qt\5.15.2\Qt5.15.2-static -confirm-license -opensource -debug-and-release -static -static-runtime -angle -combined-angle-lib -no-feature-d3d12 -platform win32-msvc -mp -nomake tests -nomake examples -nomake tools -skip qtwebengine -skip qtwebview

只编译qtbase:

-skip qt3d -skip qtactiveqt -skip qtandroidextras -skip qtcanvas3d -skip qtcharts -skip qtconnectivity -skip qtdatavis3d -skip qtdeclarative -skip qtdoc -skip qtgamepad -skip qtgraphicaleffects -skip qtimageformats -skip qtlocation -skip qtmacextras -skip qtmultimedia -skip qtnetworkauth -skip qtpurchasing -skip qtquickcontrols -skip qtquickcontrols2 -skip qtremoteobjects -skip qtscript -skip qtscxml -skip qtsensors -skip qtserialbus -skip qtserialport -skip qtspeech -skip qtsvg -skip qttools -skip qttranslations -skip qtvirtualkeyboard -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtwebview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns

编译配置:后面可以加上上面的 -skip

configure.bat -prefix g:\Qt\qt5.14.2.share -confirm-license -opensource -debug-and-release -platform win32-msvc -mp -make tools -nomake tests -nomake examples

 

参数的具体含义参考博客里说的非常详细,这里就不贴了,请自己根据需求调整。
configure -h指令可以查看允许的参数与简介

完成时会显示结果(warning,如果有的话)与下一步指示
Qt is now configured for building. Just run 'nmake'.
Once everything is built, you must run 'nmake install'.
Qt will be installed into '设定的编译结果路径'.

5 编译

继续运行nmake,需要等待比较长的时间。
当nmake完成时,继续运行nmake install,完成时就结束了。

6 添加到Qt Creator

我是用MaintenanceTool下载的Qt Creator
https://www1.qt.io/download-open-source-access/

工具-选项-Kits-Qt Versions
点击添加,找的编译结果目录bin中的qmake.exe,
会给一个找不到qmlscene.exe的警告,我的解决方案是去别的库(可以用MaintenanceTool下载)bin目录下复制个过来。

工具-选项-Kits-kits
设置好 名称,Compiler(64还是32由编译方式决定),Qt Version(上一步刚刚添加的版本)

简单写个程序测试下

 

 

关于MT静态链接的选项在

\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

即将MD改成MT,MDd改成MTd。动态改成静态

QMAKE_CFLAGS_RELEASE = -O2 -MD -Zi QMAKE_LFLAGS_RELEASE = /INCREMENTAL:NO /DEBUG

以上可以增加release版本的pdb生成

Time for another short guidance on how to build your own copy of the Qt framework from source.

I’ve upgraded to Visual Studio Community Edition 2019 (Version 16.3.10) in the meantime and will now also use the current Qt 5.14.0 release instead of the 5.12.x LTS line.

As mentioned before, building Qt from source by yourself may not be neccessary when you run up-to-date versions of the OS and IDE: Just install it via the online installer from the Qt site. I do this mostly out of habit and to keep in training 😉

Anyways, if you’re still with me, I guess you also know what/why you want it; so, on with the show.

Prerequisites and recommended installation order

  1. Microsoft Visual Studio 2019 (Community Edition) — install it at least with the C++ Desktop workload, the C++ toolset and the Windows SDK.
  2. Perl/ActivePerl — it should be put in PATH for the command prompt build environment after installation.
  3. Python — it should be put in PATH for the command prompt build environment after installation.

Official page: Qt 5 for Windows - Requirements: Building from Source.

Download

The shortcut to version Qt 5.14.0 is http://download.qt.io/official_releases/qt/5.14/5.14.0/single/qt-everywhere-src-5.14.0.zip (ca. 800 MByte).

The way to get to the download links for the offline sources from the homepage is well hidden, and the ways to get there change with every redesign every couple of months… But the final URL still works: https://www.qt.io/offline-installers/. And there: Source packages & Other releases → “The source code is available: For Windows users as a single zip file”.

Extract

Extract it to an appropriate place for you (e.g. I use C:\devel\ext\Qt\5.14.0\ in my environment).
If your extraction tool of choice puts it into sub-directories like qt-everywhere-opensource-src-5.14.0, then move the content of it up to the root level and delete this now empty folder(s).

Build

The next steps describe how you build 64-bit DLLs with Visual Studio 2019; for 32-bit files, or static libraries, or…, adjust the paths/names/options accordingly to your needs!

  1. Open the x64 Native Tools Command Prompt for VS 2019 (you can find it in the start menu entry ‘Visual Studio 2019’) and navigate to C:\devel\ext\Qt\5.14.0\ (or whatever your chosen location is…).

  2. Run Qt’s configure.bat from there to prepare the build.

    The following list of options is a setup that works for me, for my current projects; you may need different features enabled/disabled, etc.
    Run configure -h to get the full list of options.

    Note: I removed now several -no-… options (e.g. -no-opengl, -no-openssl etc.) that I used before here, because on the first attempt to build, the run stopped with a fatal link error after some hours (*groan*). Might have been a coincidence, but when doing it again, without these exclusions, it completed successfully (but also took a lot longer to build than before).

    Argument Comment
    -confirm-license Automatically acknowledge the license.
    -prefix <DIR> The deployment directory, as seen on the target device.
    <DIR> is a full path like C:\devel\ext\Qt\5.14.0\_64DLL
    -release Release-only build (Build Qt with debugging turned off).
    -opensource Build the Open-Source Edition of Qt.
    -silent Reduce the build output so that warnings and errors can be seen more easily.
    -shared Build shared Qt libraries (creating DLLs).
    -platform win32-msvc Use Microsoft Visual C++.
    -mp Use multiple processors for compilation (Microsoft Visual C++ only).
    -make tools For lupdate/linguist/lrelease and so on.
    -nomake examples Exclude examples.
    -nomake tests Exclude tests.

    That means, you’ll execute a long command, like:

    C:\devel\ext\Qt\5.14.0\> configure.bat --confirm-license -prefix g:\qt\qtcompiled -debug -opensource -shared -platform win32-msvc -mp -make tools -nomake examples -nomake tests

    `

  3. Type nmake to kick-off the building process; this will take quite a while (on my current, not very modern setup (Intel Core i5-760, 8 GB): Four to five hours…)
    To clean up (e.g. after a failed build, before trying again), do nmake clean (or nmake distclean).

  4. Type nmake install to move the built binaries to the target directory.

That’s it!

 
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部