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
動態庫用這個:configure.bat -prefix g:\Qt\qt5.14.2.share -confirm-license -opensource -debug-and-release -shared -angle -combined-angle-lib -no-feature-d3d12 -platform win32-msvc -mp -nomake tests -nomake examples -nomake tools -skip qtwebengine -skip qtwebview
清理配置:nmake confclean
清理編譯:nmake clean
或:nmake distclean
後者更徹底
make distclean類似make clean,但同時也將configure生成的文件全部刪除掉,包括Makefile。
參數的具體含義參考博客裏說的非常詳細,這裏就不貼了,請自己根據需求調整。
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。動態改成靜態
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
- Microsoft Visual Studio 2019 (Community Edition) — install it at least with the C++ Desktop workload, the C++ toolset and the Windows SDK.
- Perl/ActivePerl — it should be put in PATH for the command prompt build environment after installation.
- 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!
-
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…). -
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.
Runconfigure -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
`
-
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), donmake clean
(ornmake distclean
). -
Type
nmake install
to move the built binaries to the target directory.
That’s it!