本文主要是通過將objc4-781的源碼編譯成功,然后在源碼中進(jìn)行調(diào)試,便于跟蹤方法的底層實(shí)現(xiàn)流程
準(zhǔn)備工作
環(huán)境版本 & 最新objc源碼
- mac OS 10.15
- Xcode 11.4
- objc4-781
依賴文件下載
需要下載以下依賴文件

其中除了其中 launchd-106.10 需要在Mac OS X 10.4.4下載,其他的均可以在最新的的版本macOS 10.15中檢索到
源碼編譯
源碼編譯就是不斷的調(diào)試修改源碼的問題,主要有以下問題
問題一:unable to find sdk 'macosx.internal'

-
選擇
target -> objc -> Build Settings -> Base SDK-> 選擇 macOS 【target中的objc 和 obc-trampolines都需要更改】image
問題二:文件找不到的報(bào)錯(cuò)問題
【1】'sys/reason.h' file not found

在Apple source的
macOS10.15 --> xnu-6153.11.26/bsd/sys/reason.h路徑自行下載在
objc4-781的根目錄下新建CJLCommon文件, 同時(shí)在CJLCommon文件中創(chuàng)建sys文件-
最后將
reason.h文件拷貝到sys文件中image -
設(shè)置文件檢索路徑:選擇target -> objc -> Build Settings,在工程的Header Serach Paths中添加搜索路徑$(SRCROOT)/CJLCommonimage
【2】'mach-o/dyld_priv.h' file not found
在
CJLCommon文件中 創(chuàng)建mach-o文件-
找到文件:
dyld-733.6 -- include -- mach-o -- dyld_priv.himage -
拷貝到 mach-o文件中
image 拷貝到文件后,還需要修改
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 和 'os/base_private.h' file not found
在CJLCommon中創(chuàng)建
os文件-
找到
lock_private.h、base_private.h文件:libplatform-220 --> private --> os --> lock_private.h 、base_private.h,并將文件拷貝至 os 文件中image
【4】'pthread/tsd_private.h' file not found 和 'pthread/spinlock_private.h' file not found
在CJLPerson中創(chuàng)建 pthread 文件
-
找到
tsd_private.h、spinlock_private.h文件,h文件路徑為:libpthread-416.11.1 --> private --> tsd_private.h、spinlock_private.h,并拷貝到pthread文件image
【5】'System/machine/cpu_capabilities.h' file not found
創(chuàng)建
System -- machine文件-
找到
cpu_capabilities.h文件拷貝到machine文件,h文件路徑為:xnu6153.11.26 --> osfmk --> machine --> cpu_capabilities.himage
【6】os/tsd.h' file not found
-
找到
tsd.h文件,拷貝到os文件, h文件路徑為:xnu6153.11.26 --> libsyscall --> os --> tsd.himage
【7】'System/pthread_machdep.h' file not found
- 在這個(gè)地址下載
pthread_machdep.h文件,h文件路徑為:Libc-583/pthreads/pthread_machdep.h - 將其拷貝至
system文件中
在最新版的macOS 10.15中最新版下載的
libc中沒有這個(gè)h文件,需要下載Libc-583版本
【8】'CrashReporterClient.h' file not found
- 這個(gè)文件在改地址搜索 Libc-825.24中找到該文件,路徑為
Libc-825.24/include/CrashReporterClient.h - 導(dǎo)入下載的還是報(bào)錯(cuò),可以通過以下方式解決
- 需要在 Build Settings -> Preprocessor Macros 中加入:LIBC_NO_LIBCRASHREPORTERCLIENT
- 或者下載我給大家的文件CrashReporterClient,這里面我們直接更改了里面的宏信息 #define LIBC_NO_LIBCRASHREPORTERCLIENT
【9】'objc-shared-cache.h' file not found
-
文件路徑為:
dyld-733.6 --> include --> objc-shared-cache.himage 將h文件報(bào)備制拷貝到
CJLCommon
【10】Mismatch in debug-ness macros
-
注釋掉objc-runtime.mm中的#error mismatch in debug-ness macros
image
【11】'_simple.h' file not found
-
文件路徑為:
libplatform-220 --> private --> _simple.himage 將文件拷貝至
CJLCommon中
【12】'kern/restartable.h' file not found
在CJLCommon中創(chuàng)建
kern文件-
找到 h文件,路徑為
xnu-6153.11.26 --> osfmk --> kern -->restartable.himage
【13】'Block_private.h' file not found
-
找到 h 文件,文件路徑為
libclosure-74 --> Block_private.himage 拷貝至
CJLCommon目錄
【14】libobjc.order 路徑問題
問題描述為:can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/AppleInternal/OrderFiles/libobjc.order
選擇
target -> objc -> Build Settings-
在工程的
Order File中添加搜索路徑$(SRCROOT)/libobjc.orderimage
【14】Xcode 腳本編譯問題
問題描述為:/xcodebuild:1:1: SDK "macosx.internal" cannot be located.
選擇
target -> objc -> Build Phases -> Run Script(markgc)-
把腳本文本
macosx.internal改成macosximage
編譯調(diào)試
-
新建一個(gè)
target:CJLTestimage -
綁定二進(jìn)制依賴關(guān)系image
源碼調(diào)試
-
自定義一個(gè)CJLPerson類
image -
在main.m中 創(chuàng)建 CJLPerson的對象,進(jìn)行源碼調(diào)試
image



















