前言
自从XCode系统升级到macOC Monterey,以往的Objc4-818.2源码就不能跑了,最近发现苹果更新了objc4-838 源码,于是迫不及待的来一波编译。
准备工作
-
- 下载 objc4-838 源码
-
- 下载 dyld-940 源码
-
- 下载 libc-1506.40.4 源码
-
- 下载 Libc-825.40.1 源码
-
- 下载 libclosure-79 源码
-
- 下载 libplatform-273.40.1 源码
-
- 下载 libpthread-485.40.4 源码
-
- 下载 xnu-8019-41.5 源码
编译环境
MAC 2015macOC Monterey+Xcode 13
编译前修改
打开objc4工程
1. 修改脚本
-
在
Targets->Run Script (markgc)将macosx.internal改成macosx:
2. 修改Base SDK
-
在
Project->build setting->Base SDK,选择macOS:
3. 选择编译目标
编译并添加缺失库
-
选择
Targets->objc -
在项目根目录创建一个文件,本项目里创建的叫
WSLib,在Targets->Build Settings->Header Search Paths配置WSLib路径: -
下面进行编译并处理报错
1. 'sys/reason.h' file not found
-
在下载的源码
xnu-main的bsd->sys中,找到reason.h并复制,并在自己创建的目录WSLib中创建sys文件,然后粘贴reason.h文件 -
继续编译
2. 'mach-o/dyld_priv.h' file not found
-
在下载的
dyld-main的目录include->mach-o中找到dyld_priv.h文件并复制,然后在WSLib中创建mach-o文件,并粘贴dyld_priv.h -
打开
dyld_priv.h文件,并添加宏定义:#define DYLD_MACOSX_VERSION_10_11 0x000A0B00 #define DYLD_MACOSX_VERSION_10_12 0x000A0C00 #define DYLD_MACOSX_VERSION_10_13 0x000A0D00 #define DYLD_MACOSX_VERSION_10_14 0x000A0E00 -
继续编译
3. 'os/lock_private.h' file not found
-
在下载的源码
libplatform-main中,private->os中找到lock_private.h文件并复制,然后在WSLib中创建os文件,并粘贴lock_private.h -
继续编译
4. Expected ',' 报错(dyld_priv.h文件中)
-
删除文件中所以报错处的
bridgeos(3.0)参数与前面的,: -
删除后,继续编译
5. 'os/base_private.h' file not found
-
在下载的源码
libplatform-220.100.1中,在目录private->os中找到base_private.h文件并复制,并粘贴到WSLib中的os文件 -
继续编译
6. 'pthread/tsd_private.h' file not found
- 此处报错在
lock_private.h文件中,里面有两个错:- 首先要去掉文件里面的
bridgeos(4.0)参数 - 其次在下载的
libpthread-main源码中,在目录private->pthread中找到tsd_private.h并复制,然后在WSLib中创建pthread目录,并粘贴文件
- 首先要去掉文件里面的
- 继续编译
7. 'System/machine/cpu_capabilities.h' file not found
-
在下载的源码
xnu-main中,在目录osfmk->machine找到cpu_capabilities.h文件并复制,然后在WSLib中创建System文件,然后在System文件中创建machine文件,并粘贴cpu_capabilities.h -
继续编译
8.'os/tsd.h' file not found
-
在下载的源码
xnu-main中,在目录libsyscall->os中找到tsd.h并复制,然后在WSLib中的os文件中粘贴 -
继续编译
9. 'pthread/spinlock_private.h' file not found
-
在下载的源码
libpthread-main中,在目录private->pthread中找到spinlock_private.h文件并复制,然后粘贴到WSLib中的pthread文件中 -
继续编译
10. 'System/pthread_machdep.h' file not found
-
在下载的文件
Libc-825.40.1中,在目录pthreads中找到pthread_machdep.h文件并复制,并粘贴到WSLib->System目录 -
继续编译
11. 'CrashReporterClient.h' file not found
-
在下载的文件
Libc-825.40.1中,找到CrashReporterClient.h文件并复制,然后粘贴到WSLib文件中- 此时编译还会报错,有两种方案:
-
- 在
CrashReporterClient.h中添加宏定义
#define LIBC_NO_LIBCRASHREPORTERCLIENT - 在
-
- 在
Targets->objc->Build Settings->Preprocessor Macros中,添加LIBC_NO_LIBCRASHREPORTERCLIENT
- 在
-
- 此时编译还会报错,有两种方案:
-
继续编译
12. 注释declaration of 和 Typedef redefinition with
- 此处报错在
pthread_machdep.h文件中,有四处-
Typedef redefinition with different types ('int' vs 'volatile OSSpinLock' (aka 'volatile int'))
-
Static declaration of '_pthread_has_direct_tsd' follows non-static declaration
-
Static declaration of '_pthread_getspecific_direct' follows non-static declaration
-
Static declaration of '_pthread_setspecific_direct' follows non-static declaration将这些报错全部注释掉
-
- 继续编译
13. 'os/feature_private.h' file not found
- 直接注释掉
14. Use of undeclared identifier 'dyld_fall_2020_os_versions'
-
注释掉代码相关的两行
15. Use of undeclared identifier 'objc4'
-
注释掉相关代码
16. Use of undeclared identifier 'dyld_platform_version_macOS_10_13'
-
注释掉这块代码
17. Use of undeclared identifier 'dyld_platform_version_macOS_10_11'
-
注释掉相关条件
18. 'objc-shared-cache.h' file not found
-
在下载的源码
dyld-main中,在目录include中找到objc-shared-cache.h并复制,然后粘贴到WSLib中的 -
继续编译
19. Use of undeclared identifier 'dyld_fall_2018_os_versions'
-
注释相关条件
-
继续编译
20. '_simple.h' file not found
- 在下载的源码
libplatform-main中的private目录找到并复制,然后粘贴到WSLib中
21. 'os/linker_set.h' file not found
- 在下载的源码
xnu-main中,在目录bsd->sys中找到linker_set.h,并复制到WSLib中的os目录
22. 'Cambria/Traps.h' file not found
-
注释掉代码
#include <Cambria/Traps.h> #include <Cambria/Cambria.h>
23. 'Block_private.h' file not found
- 在下载的源码
libclosure-main中找到Block_private.h,并复制到WSLib目录
25. 'kern/restartable.h' file not found
- 在下载的源码
xnu-main中,在目录osfmk->kern中找到restartable.h文件并复制,然后在WSLib中创建kern目录并粘贴restartable.h文件
26. oah_is_current_process_translated与objc_thread_get_rip报错
-
注释该条件
27. 'os/feature_private.h' file not found
- 注释掉头文件的引用
28. 'os/reason_private.h' file not found
- 在下载的源码
xnu-main中,在目录libkern->os中找到reason_private.h文件,并粘贴到WSLib中的os文件中
29. 'os/variant_private.h' file not found
-
在下载的源码
Libc-main中的os目录找到variant_private.h,并复制到WSLib中的os目录 -
在
variant_private.h中,删除bridgeos参数
30. Use of undeclared identifier 'dyld_platform_version_bridgeOS_2_0'
-
注释掉这块代码
31. '_static_assert' declared as an array with a negative size
-
注释掉这两行
32. library not found for -lCrashReporterClient
-
进入
Targets->Build Setting->Other Linker Flags中,删除-lCrashReporterClient
33. library not found for -loah
-
进入
Targets->Build Setting->Other Linker Flags中,删除-loah
34. Unknown type name 'uint32_t', Unknown type name 'uint64_t, Use of undeclared identifier 'INT64_C'
- 进入根目录
Runtime->llvm-MathExtras.h, 在导入头文件#include <cstdint>即可解决
编译成功 🎉🎉🎉
添加Target
-
先点击
Targets下面的+: -
再选择
macOS->command Line Tool,此处取名WSBuild -
添加依赖库
libobjc.A.dylib:
编译WSBuild(新Target)
-
先选择
WSBuild:
修复断点不走问题
-
- 在
Build Phases的Compile Sources中,将main.m拖到最前面
- 在
-
- 找到
Targets->Build Settings->Enable Hardened Runtime,值置为NO
- 找到
设置完,断点就可以走了:
已知M1电脑问题修复
- 将
objc-cache.mm中的报错代码注释:
温馨提示✨
- 此源码在
macOC Monterey可运行,低于macOC Monterey的系统可使用 objc4-818.2源码,需要自行编译。 - 最后附上编译好的 Demo