不建议从源码构建!除非你没 其他办法 了
构建费事费力,需要踩过很多坑才能得到一个勉强能用的版本。
必要性
如果要在不联网的设备上使用新版本 Qt ,还是需要掌握如何编译 Qt 源码!
断点调试:之前想断点进 Qt 源码调试一直没成功,借这次机会一并实现。
Qt 4.8.7
Qt4.8.7 安装包,自带源码和 pdb 调试文件,结合 MSVC2010 断点调试理解源码。
比查看、学习 Qt5.x 的源码方便很多:但这个版本终究是太老了,
- Qt5.15.x 没有离线安装包
- Qt5.12.x 虽然有离线安装包,但没有 pdb 调试文件
- 单独下载 pdb 文件后与源码版本不一致,断点位置偏移
使用 Qt Creator 默认的模板创建项目,调试时提示“调试器未设置”,所以使用 MSVC2010 。
安装 qt-vs-addin-1.2.5.exe
构建过程
参考 Building from Source ,需要 Perl / Python / MSVC ,其他章节跳过不用看。
参考 Qt for Windows - Building from Source ,5 个步骤,按部就班做就可以:
Step1 不需要商业许可
Step2 使用 qt-everywhere-src-5.15.x
Step3 配置环境
- 配置 clang 可以先跳过;
- ICU 和 ANGLE 三方库并不是必要项目;
- 其他中文教程中常见的 perl / python 也不需要;
- SSL 可能也不需要,但我电脑之前其他项目需要已经配置了,未作关注
1 | REM Set up Microsoft Visual Studio 2017, where <arch> is amd64, x86, etc. |
Step4 构建
提供的配置命令太保守,我们可以更激进一些,将不需要的目录全部跳过。
参考 Qt Configure Options ,尽可能地减少模块和特性,只保留需要的,减少构建时长
1 | ..\qt-everywhere-src-5.15.15\configure.bat -release -recheck-all -opensource^ |
即便如此,在 x240 上通过 nmake 构建依然在一小时以上(报错中止了)
改用 jom ,半小时构建完毕。
jom is a clone of nmake to support the execution of multiple independent commands in parallel.
只构建 qtbase
调整 configure.bat 的各项参数删减模块太麻烦,可以只下载 qtbase 模块源码,从源头杜绝编译其他模块的可能性。
编写脚本完成下载-构建-安装 Qt 5.15.x,用于学习 Qt 核心的元对象系统、事件循环、信号和槽函数。
Step5 帮助文档
构建 QtDoc 搁置,未成功。
参考 Building Qt 5 Documentation ,Qt 提供了已经编译好的 clang 库备用,但有坑,/(ㄒoㄒ)/~~ ;
参考 Installing Clang for QDoc 配置环境
Step6 Qt Creator
官方提供 Qt Creator ,直接下载即可。
不用 CDB 调试:
使用 Qt Creator 调试 msvc 构建的程序时,必须安装 CDB :
没有单独的安装包,要把 WDK 全部装上,占用 2.1G (lll¬ω¬)
那我只用 Qt Creator 借用 gdb 调试
我改用 MSVC 调试 qt 程序就不用 cdb 了吧?
libclang
构建 qdoc.exe 对 clang 版本比较挑剔;构建 QtDoc 非常耗时,暂未成功。
- clang 18.1.7 failed 版本太高,Qt5.15 识别不出来 ;
- clang 12.0 ok ;
- clang 11.0 ok;
- clang 10.0 build qdoc.exe ok ,构建 QtDoc 时 vc2019 报错:版本太低
静态库
前述链接目录下中 libclang.lib 似乎是 静态库,不存在 bin\libclang.dll ,使用其中的包构建 qdoc.exe 报错:
libclang-release_18.1.7-based-windows-vs2019_64.7z 05-Jul-2024 11:00 461M
f0cffd0e9cff950af20efc36d42aefdb libclang-release_18.1.7-based-windows-vs2019_64.7z
libclang-release_120-based-windows-vs2019_64.7z 30-Apr-2021 10:37 306M
86c307866f629b0fb4fa1df801c99268 libclang-release_120-based-windows-vs2019_64.7z
libclang-release_110-based-windows-vs2019_64.7z 20-Oct-2020 20:57 290M
ff0a30c881691068c14fbed9239b3583c8c45c6a libclang-release_110-based-windows-vs2019_64.7z
使用 clang12.0 静态库构建 qdoc.exe 工具时报错:

在 [How can I get rid of the imp prefix in the linker in VC++? 中也提到:__imp_ 是动态库特有的前缀。
动态库
上一层 目录 中的 libclang.lib 是 动态库 配套的导入库,存在 bin\libclang.dll ,使用特定的版本构建 qdoc.exe 成功:
libclang-release_18.1.7-based-windows-vs2019_64.7z 12-Jun-2024 12:50 628M
ceecf8830149829d1269bf0008319c34 libclang-release_18.1.7-based-windows-vs2019_64.7z
libclang-release_120-based-windows-vs2019_64.7z 03-May-2021 11:57 364M
e6a729d347b5cedb3391a59ca695dcfe libclang-release_120-based-windows-vs2019_64.7z
libclang-release_110-based-windows-vs2019_64.7z 23-Mar-2021 10:44 342M
488375c83ea2d808d1b2e96e1ca68a8b libclang-release_110-based-windows-vs2019_64.7z
libclang-release_100-based-windows-vs2019_64.7z 18-Sep-2020 10:09 317M
MD5 Hash: 00aac61180028608e421c7a02b798b24
使用 clang10.0 动态库构建文档时报错:

构建过程
qmake 基于 .pro 项目文件生成 makefile 。
qmake generates a Makefile based on the information in a project file.
nmake.exe 基于 makefile 中描述的指令构建项目。
The Microsoft Program Maintenance Utility (NMAKE.EXE) is a command-line tool included with Visual Studio. It builds projects based on commands that are contained in a description file, usually called a makefile.
jom.exe 基于 nmake 增加了并发构建的功能。
jom is a clone of nmake to support the execution of multiple independent commands in parallel.
cl.exe 用来控制 c/c++ 编译器和链接器。和 gcc/g++ 用途一致。
cl.exe is a tool that controls the Microsoft C++ (MSVC) C and C++ compilers and linker. cl.exe can be run only on operating systems that support Microsoft Visual Studio for Windows.
uic.exe 将 .ui 用户界面文件转换成 .h 头文件。
The uic reads an XML format user interface definition (.ui) file as generated by Qt Designer and creates a corresponding C++ header file or Python source file.
moc.exe 元对象编译器,用于处理 Qt 的扩展语法。
The
moctool reads a C++ header file. If it finds one or more class declarations that contain theQ_OBJECTmacro, it produces a C++ source file containing the meta-object code for those classes
cannot run ‘rc.exe’
在安装 Qt 5.12.12 + MSVC 2015 之后,再次安装 MSVC2019 ,可能会碰到 LNK1158: cannot run ‘rc.exe’ 错误造成编译失败。
社区关于此问题的 讨论和 workaround
排查发现 echo %WindowsSdkDir% 环境变量指向的 C:\Program Files (x86)\Windows Kits\10\ 子目录内下没有 rc.exe