简 述: 在 Mac 上编译 qt 5.15 源码,debugrelease 版本。感觉每一个学 C++/Qt 的人,大概都会早晚都会这么一个念头,去编译一下 Qt 源码,只是有去尝试或者没有尝试过罢了。本文提供一个详细的编译全程和原文翻译。

[TOC]


本文初发于 “偕臧的小站” ifmet.cn,同步转载于此。


背景

​ 下载最新的 Qt 源码(5.15.2)来编译一下,编译一个 release 和 debug 的最新代码;当然,也可以只编译所有源码其中的一些子模块;或者直接下载子模块代码;和再编译一下 QtCreator ;源码官方的下载地址
​ 这里将 qt-everywhere 和 qtbase 的源码都编译一下;且分别编译一下

首先可以参考对应的官方编译说明:


README 上说:

  • Perl 5.8或更高版本

  • Python 2.7或更高版本

  • 支持C++11标准的C++编译器


实际本机环境: MacOS 10.14.6 📎 Command_Line_Tools_for_Xcode_11.3.1 📎 Perl v5.32.1 📎Python 3.9.2 📎 支持 c++11 标准

处理器: 2.3 GHz 四核 Intel Core i5 (后面提供编译耗时参考)


编译 QtEveryWhere 源码

编译 Release 版本

下载 qt 全部源码: qt-everywhere-src-5.15.2.tar.xz

tar -zxvf qt-everywhere-src-5.15.2.tar.xz
cd qt-everywhere-src-5.15.2
./configure -prefix ~/Qt/qteverywhere5.15.2 -release -opensource -nomake tests
make -j8                                   # 耗时 52 min
make install                               # 安装到 ~/Qt/qteverywhere5.15.2 下,共 689.7 MB 

注意:

执行 ./configure 脚本时(第 3 行): ~/Qt/qteverywhere5.15.2 为执行 make install 的安装路径;然后执行命令脚本,生成的临时文件都在 .obj/release/

若是编译 release 版本加 -nomake example会失败,应去掉;若是编译 debug 版本可加 -nomake example,可顺利编译成功; ,遇到如下选择,按下 y 后回车;确认使用开源许可证即可

Type 'L' to view the GNU Lesser General Public License version 3 (LGPLv3).
Type 'G' to view the GNU General Public License version 2 (GPLv2).
Type 'y' to accept this license offer.
Type 'n' to decline this license offer.
 
Do you accept the terms of either license? y

执行 make -j8 时候,看到如下,即表示编译成功

/Library/Developer/CommandLineTools/usr/bin/clang++ -stdlib=libc++ -headerpad_max_install_names  -arch x86_64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -mmacosx-version-min=10.13 -Wl,-rpath,@executable_path/../Frameworks -Wl,-rpath,@loader_path/../../../../../../lib -o minibrowser.app/Contents/MacOS/minibrowser .obj/main.o .obj/qrc_qml.o   -F/Users/xmuli/project/qt-build/qt-everywhere-src-5.15.2/qtdeclarative/lib -F/Users/xmuli/project/qt-build/qt-everywhere-src-5.15.2/qtwebview/lib -F/Users/xmuli/project/qt-build/qt-everywhere-src-5.15.2/qtbase/lib -framework QtQuick -framework QtWebView -framework QtGui -framework AppKit -framework Metal -framework QtQmlModels -framework QtQml -framework QtNetwork -framework QtCore -framework DiskArbitration -framework IOKit -framework OpenGL -framework AGL 

验证 Qt 编译成功

下载 mac 版本的 qtcreator4.14.0 ,安装后打开。选中 “首选项-Kits-Qt Versions” ,将我们安装好的路径拷贝进去,保存 OK;


然后切换到 **“首选项-Kits-Kits”** ,选中 Desktop 64bit 那一个,然后在其中 Qt version 中下拉,选取我们刚才的设置的 Qt 版本,保存;
再创建一个默认的 test 空的 qwidget 空测试项目,其中编译套件选中我们刚才所选;

点击 IDE 的编译,运行,即可成功


编译 QtBase 源码

编译 Release 版本

下载 qtbase 源码:qtbase-everywhere-src-5.15.2.tar.xz

tar -zxvf qtbase-everywhere-src-5.15.2.tar.xz
cd qtbase-everywhere-src-5.15.2/
./configure -prefix ~/Qt/qtbase5.15.2 -release -opensource -nomake tests
make -j8                                        # 14:44
make installs                                   # 安装到 ~/Qt/qtbase5.15.2 下

PS: 第三行中:

编译成功,查看到 ~/Qt/qtbase5.15.2 有很多新文件夹;


编译 Debug 版本

ERROR: debug-only framework builds are not supported. Configure with -no-framework if you want a pure debug build.

由于上面提示,framework 模块不支持 debug 模式,故编译命名改为如下:

tar -zxvf qtbase-everywhere-src-5.15.2.tar.xz
cd qtbase-everywhere-src-5.15.2
./configure -prefix ~/Qt/qtbase5.15.2.debug -debug -opensource -nomake tests -nomake examples -no-framework
make -j8                                   # 耗时约 8 min 
make install                               # 安装到 ~/Qt/qtbase5.15.2.debug 下,共 159.8 MB

编译成功,查看到 ~/Qt/qtbase5.15.2.debug 有很多新文件夹,欲想验证 qtbase 编译成功否,可参考如上。


截图成功

很棒,编译的几个版本全部都过了,来一张全家福纪念一下;后面会再重新编译一下 Qt Creator 的源码;虽然上面两个有⚠️符号,但是一样可以编译项目通过


若遇报错注意:

若遇报错如下:

# -debug-and-release
clang: error: no such file or directory: '/Users/xmuli/project/qt-build/qtbase-everywhere-src-5.15.2/lib/libqtpcre2_debug.a'
make[3]: *** [../../lib/QtCore.framework/QtCore_debug] Error 1
make[2]: *** [debug-release-install] Error 2
make[1]: *** [sub-corelib-install_subtargets] Error 2
make: *** [sub-src-install_subtargets] Error 2

# -release
clang: **error:** no such file or directory: '/Users/xmuli/project/qt-build/qtbase-everywhere-src-5.15.2/lib/libqtpcre2.a'
make[2]: *** [../../lib/QtCore.framework/QtCore] Error 1
make[1]: *** [sub-corelib-install_subtargets] Error 2
make: *** [sub-src-install_subtargets] Error 2

原因: 执行了make install -j8 ,一边编译,一边安装,造成了依赖关系顺序的问题,就会报错如上

make -j4 qt-everywhere 需要 1h 30 min,博主亲测;建议买个好的 CPU,人生时间不应浪费在等待上,莫要如我


系列:

QtExamples

欢迎 starfork 这个系列的 QT / DTK 学习,附学习由浅入深的目录。