首页 Qt源码解析系列1:源码编译for macos arm64
文章
取消

Qt源码解析系列1:源码编译for macos arm64

使用Qt5.15.2版本。

源码解压后最好放在~/Qt/5.15.2/Src目录下,这样当把编译出来的库放到~/Qt/5.15.2/clang_64目录下时,就可以直接调试源码了,不然还需要映射源码路径。

编译

注:arm的需要在arm机器上编译

1
./configure -prefix ./arm64 -opensource -force-debug-info -nomake tests QMAKE_APPLE_DEVICE_ARCHS=arm64 -skip qtdoc

报错:

1
2
3
4
qiosurfacegraphicsbuffer.h:54:32: error: unknown type name 'CGColorSpaceRef'
解决方案:找到qiosurfacegraphicsbuffer.h,我的文件目录如下
qt-everywhere-src-5.15.5/qtbase/src/plugins/platforms/cocoa/qiosurfacegraphicsbuffer.h
46行添加头文件:#include <CoreGraphics/CGColorSpace.h>

又报错

1
2
3
4
5
6
../../../3rdparty/assimp/contrib/zip/src/miniz.h:3075:30: error: variable has incomplete type 'struct stat64'
解决方案:
搜索miniz.h文件,共2个,然后将代码中 stat64 改成 stat
./qtquick3d/src/3rdparty/assimp/src/contrib/zip/src/miniz.h
./qt3d/src/3rdparty/assimp/contrib/zip/src/miniz.h 
看来是qt3d用到。也可以添加 -skip qt3d -skip qtquick3d 解决

如何编译出dSYM符号表

需要添加 -separate-debug-info

报错:

1
2
3
Error copying  Cannot open qttools/bin/qcollectiongenerator.dSYM/Contents/Resources/DWARF/qcollectiongenerator for input
qcollectiongenerator这个编译不出来,这个是qttools里面的assistant里面依赖的。
可以添加 -skip qttools,不过就编译不出来lrelease工具了。

通过

1
2
 ./configure  -list-features
 -no-feature-<feature>  Disable <feature> 

可知可以用-no-feature-assistant选项跳过assistant,

同时也可以添加 -nomake tests -nomake examples #根据名字可以猜测,examples,tests有可能就是qtbase下那些文件夹的名字,

综上

1
2
3
4
5
6
7
8
9
10
先 vim qtbase/src/plugins/platforms/cocoa/qiosurfacegraphicsbuffer.h
在46行添加头文件:#include <CoreGraphics/CGColorSpace.h>

然后在arm机器上编译:
./configure -prefix ./arm64 -release -opensource -force-debug-info -separate-debug-info -nomake tests -nomake examples  QMAKE_APPLE_DEVICE_ARCHS=arm64 -skip qtdoc -skip qt3d -skip qtquick3d -skip qtwebengine -no-feature-assistant  

在intel机器上编译:
./configure -prefix ./x86_64 -release -opensource -force-debug-info -separate-debug-info -nomake tests -nomake examples  QMAKE_APPLE_DEVICE_ARCHS=x86_64 -skip qtdoc -skip qt3d -skip qtquick3d -skip qtwebengine -no-feature-assistant
make -j12
注意最后用make -j1 install #安装时最好用j1防止竞争

合并成universal库

QMAKE_APPLE_DEVICE_ARCHS使用”x86_64h;arm64”,会编译失败,需要一个平台一个平台地编译(intel的在intel机器上编译)。

GitHub上有个工具 https://github.com/nedrysoft/makeuniversal,用它可以合并两个文件夹为universal库

1
makeuniversal universal arm64 x86_64 # 3个都是根目录 

其中 x86_64为编译出来的x86版本根目录,arm64 为编译出来的的arm64版本根目录,命令执行成功后 universal目录下就是支持两个平台的Qt工具集.

验证

看看是否生成 qmake,lrelease工具

为什么要改成stat

1
2
3
4
5
6
7
8
9
10
11
12
#include <sys/stat.h> 里面有 

#if __DARWIN_64_BIT_INO_T
struct stat __DARWIN_STRUCT_STAT64;

#if !__DARWIN_ONLY_64_BIT_INO_T
struct stat64 __DARWIN_STRUCT_STAT64;
可以看到都指向了同一个结构体,所以将stat64 改成 stat可行。
```cpp
qt-everywhere-src-5.15.2/qt3d/src/3rdparty/assimp/contrib/zip/src/miniz.h:3075:30 ,在官方bug上:
<https://bugreports.qt.io/browse/QTBUG-98826> 有提到
-skip qt3d -skip qtquick3d  -skip qtwebengine  #根据qt3d名字可以猜测,有可能就是根目录下文件夹的名字,比如qttools

参考

https://www.jianshu.com/p/1d879b42a5d8

https://doc.qt.io/qt-5/macos-building.html

https://doc.qt.io/qt-6/macos.html#architectures

https://blog.csdn.net/ykun089/article/details/120430329

https://www.cnblogs.com/wqcwood/p/15138983.html

本文由作者按照 CC BY 4.0 进行授权