由于电脑还没有升级到最新版本,所以本文章没有用当前最新源码,进行调试。只是选取了macOS 10.15。其实调试内容基本一样,除个别报错外。
准备工作
- mac OS 10.15
- Xcode 12.4
- objc4-779.1


在 Apple source 获取到。
报错解决
错误一:运行源码后,会提示2个同样错误,系统要求的macOS版本需要处理。
unable to find sdk 'macosx.internal'
Traditional headermap style is no longer supported; please migrate to using separate headermaps and set 'ALWAYS_SEARCH_USER_PATHS' to NO.

点击选择 target -> objc -> Build Settings -> Base SDK -> 选择 macOS 让系统给你进行匹配

错误二:文件查漏补缺
'sys/reason.h' file not found

缺失的文件我们可以去 Apple source ->xnu-6153.11.26下载,然后找到reason.h文件

- 我在根目录创建了一个
common文件 - 创建
sys文件 - 把
reason.h文件加入进去
解决一.png
目前还不行,一定给我们的工程设置文件检索路径
- 选择
targets->objc->Build Settings - 在工程的
Header Serach Paths中添加搜索路径$(SRCROOT)/common

'mach-o/dyld_priv.h' file not found
'os/lock_private.h' file not found
'os/base_private.h' file not found
'pthread/tsd_private.h' file not found
'System/machine/cpu_capabilities.h' file not found
'os/tsd.h' file not found
'pthread/spinlock_private.h' file not found
'System/pthread_machdep.h' file not found
'CrashReporterClient.h' file not found
'objc-shared-cache.h' file not found
'_simple.h' file not found
'Block_private.h' file not found
上面的报错情况处理方式都是和 'sys/reason.h' file not found 一样的解决,
- dyld-732.8/include/mach-o/dyld_priv.h
- libplatform-220/private/os/lock_private.h,base_private.h
- libpthread-416.11.1/private/tsd_private.h,spinlock_private.h
- xnu-6153.11.26/osfmk/machine/cpu_capabilities.h
- xnu6153.11.26/libsyscall/os/tsd.h
- Libc-1353.11.2/pthreads/pthread_machdep.h
- Libc-1353.11.2/include/CrashReporterClient.h
- dyld-732.8/include/objc-shared-cache.h
- xnu-6153.11.26/osfmk/kern/restartable.h
- libplatform-220/private/_simple.h
- libclosure-74/Block_private.h
错误三:dyld_priv 文件修改
Expected ','
bridgeos(3.0)报错

将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


错误四:CrashReporterClient异常
如果直接导入 CrashReporterClient.h 还是会报错:
'CrashReporterClient.h' file not found

需要在
Build Settings -> Preprocessor Macros 中加入:LIBC_NO_LIBCRASHREPORTERCLIENT
错误五:Mismatch in debug-ness macros

注释掉
objc-runtime.mm中的#error mismatch in debug-ness macros
错误六:
Static_assert failed due to requirement 'bucketsMask >= ((unsigned long long)140737454800896ULL)' "Bucket field doesn't have enough bits for arbitrary pointers."

直接注释掉该断言。
错误七:
can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/AppleInternal/OrderFiles/libobjc.order

在工程中配置: targets -> objc -> Build Settings -> Order File中添加路径$(SRCROOT)/libobjc.order

错误八:
library not found for -lCrashReporterClient

在工程中配置: targets -> objc -> Build Settings -> Other Linker Flags Debug与Release模式下的Any macOS SDK 找到lCrashReporterClient删除掉.

错误九:
'_static_assert' declared as an array with a negative size

直接注释掉该断言。
错误十:
SDK "macosx.internal" cannot be located.

在工程中配置: targets -> Build Phases -> Run Script中的macosx.internal改成macosx

上面的所有问题解决完,objc4源码工程就能编译成功了,完整的工程文件如下:

参考:
https://www.jianshu.com/p/bffba01690d7
https://www.jianshu.com/p/5809940f77d9
https://juejin.cn/post/6844904082226806792
