后續(xù)探索,基于macOS 10.15.1
版本發(fā)布的objc4-781
源碼决摧。
但是源碼無法直接運(yùn)行和編譯政基,我們需要搭建可編譯環(huán)境。
如果想走 快捷通道胜蛉,可下載Cooci老師的
可編譯源碼
: 運(yùn)行objc4-781
版本
開發(fā)環(huán)境
- macOS 10.15.6
- Xcode 11.7
- objc4-781
下載資源
1. objc4-781源碼
下載方法一:
蘋果開源源碼匯總: https://opensource.apple.com挠进,在macOS -> 10.15.1
版本中色乾,搜索objc4
,直接下載objc4-781
领突。
下載方法二:
直接地址: https://opensource.apple.com/tarballs/暖璧,搜索objc4
。進(jìn)入君旦,找到objc4-781
并下載澎办。
2. 依賴文件
在蘋果開源源碼頁面,除了lauchd-106.10
需要在macOS -> 10.4.4.x86
版本中下載金砍。 其余均可在macOS -> 10.15.1
版本中搜索到局蚀。
3. 編譯源碼
這是個(gè)痛苦的過程,得不斷調(diào)試和修改源碼問題恕稠。
打開objc.xcodeproj
,選中objc
target琅绅,開始編譯:
問題一: unable to find sdk 'macosx.internal'
處理:
-
target -> objc -> build Setings ->Architectures
的BaseSDK
選中macOS 10.15
-
target -> objc-trampolines -> build Setings -> Architectures
的BaseSDK
選中macOS 10.15
問題二:一系列缺失文件的問題
1. 'sys/reason.h' file not found
在下載的依賴文件夾中,找到文件:
xnu-6153.41.3 -> bsd -> sys -> reason.h
在
objc4-781
的根目錄下新建HTCommon
文件夾鹅巍,在HTCommon中創(chuàng)建sys
文件夾千扶,將reason.h
文件拷貝到sys
文件夾中
- 配置
文件索引
路徑:
target
->objc
->Build Settings
搜索header_search Paths
, 添加$(SRCROOT)/HTCommon
2.'mach-o/dyld_priv.h' file not found
- 在
HTCommon
文件夾中,創(chuàng)建march-o
文件夾 - 在下載的依賴文件夾中骆捧,找到文件:
dyld-733.6 -> include -> mach-o -> dyld_priv.h
,復(fù)制到HTCommon/march-o
文件夾中
3. 'os/lock_private.h' file not found
- 在
HTCommon
文件夾中澎羞,創(chuàng)建os
文件夾 - 在下載的依賴文件中,找到文件:
libplatform-220 --> private --> os --> lock_private.h 和 base_private.h
敛苇,復(fù)制到HTCommon/os
文件夾中
4. dyld_priv.h 報(bào)了一堆 Expected ',' 錯(cuò)誤
- 我們暫時(shí)直接移除
bridgeos(3.0)
妆绞,世界瞬間安靜了
5. lock_private.h 報(bào) Expected ',' 錯(cuò)誤
- 繼續(xù)移除報(bào)錯(cuò)的
移除
bridgeos(3.0)`
6. 'pthread/tsd_private.h' file not found
- 在
HTCommon
文件夾中,創(chuàng)建pthread
文件夾 - 找到文件:
libpthread-416.40.3 --> private --> tsd_private.h 和 spinlock_private.h
,復(fù)制到HTCommon/pthread
文件夾中
7. 'System/machine/cpu_capabilities.h' file not found
- 在
HTCommon
文件夾中枫攀,創(chuàng)建System
文件夾,System文件夾中創(chuàng)建machine
文件夾 - 找到文件:
xnu-6153.41.3 --> osfmk --> machine --> cpu_capabilities.h
,復(fù)制到HTCommon/System/machine
文件夾中
8. 'os/tsd.h' file not found
- 找到文件:
xnu-6153.41.3 --> libsyscall --> os --> tsd.h
,復(fù)制到HTCommon/os
文件夾中
9. 'System/pthread_machdep.h' file not found
-
在這里下載找到
Libc-583/pthreads/pthread_machdep.h
, 將其復(fù)制到HTCommon/System
文件夾摆碉。
手動(dòng)復(fù)制吧 ??
在最新版的macOS 10.15中最新版下載的
libc
中沒有這個(gè)h文件,需要下載Libc-583
版本
10. 'CrashReporterClient.h' file not found
-
在這里復(fù)制
CrashReporterClient.h
, 將其復(fù)制到HTCommon
文件夾脓豪。 - 在
Build Settings
搜索Preprocessor Macros
, 加入:LIBC_NO_LIBCRASHREPORTERCLIENT
11. 'objc-shared-cache.h' file not found
- 找到文件:
dyld-733.6 --> include --> objc-shared-cache.h
,復(fù)制到HTCommon
文件夾中
12. pthread_machdep.h文件報(bào)了一堆錯(cuò)
- 不要慌巷帝,將193行至244行替換以下內(nèi)容,先運(yùn)行起來
#if TARGET_IPHONE_SIMULATOR || defined(__ppc__) || defined(__ppc64__) || \
(defined(__arm__) && !defined(_ARM_ARCH_7) && defined(_ARM_ARCH_6) && defined(__thumb__))
#define _pthread_getspecific_direct(key) pthread_getspecific((key))
#define _pthread_setspecific_direct(key, val) pthread_setspecific((key), (val))
#else
#endif
13. Use of undeclared identifier 'DYLD_MACOSX_VERSION_10_13'
- 我們在
HTCommon/mach-o/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
14. 'Block_private.h' file not found
- 找到文件:
libclosure-74 -> Block_private.h
扫夜,復(fù)制到HTCommon
文件夾中
15. '_simple.h' file not found
- 找到文件:
libplatform-220 -> private -> _simple.h
楞泼,復(fù)制到HTCommon
文件夾中
16.'kern/restartable.h' file not found
- 在
HTCommon
文件夾中,創(chuàng)建kern
文件 - 找到文件:
xnu-6153.41.3 -> osfmk -> kern -> restartable.h
笤闯,復(fù)制到HTCommon/kern
文件夾中
17.can't open order file
- 選擇
target -> objc -> Build Settings
- 在工程的
Order File
中添加搜索路徑$(SRCROOT)/libobjc.order
18.library not found for -lCrashReporterClient
- 選擇
target -> objc -> BuildSettings
- 搜索
Other Linker Flags
堕阔, 刪除lCrashReporterClient
(Debug
和Release
都刪)
19. SDK "macosx.internal" cannot be located. 腳本編譯問題
- 選擇
target -> objc -> Build Phases -> Run Script(markgc)
- 將
macosx.internal
改為macosx
編譯成功!恭喜你颗味! ??
源碼已擼好超陆,配置新target,開啟你的探索之路:
1. 新建Target
: HTTest
- 綁定二進(jìn)制依賴關(guān)系
- 運(yùn)行
HTTest
代碼,自由編譯調(diào)試
新世界的通道已搭建穩(wěn)定时呀。既然OC是面向?qū)ο蟮恼Z言张漂,那就讓我們從萬物始源alloc講起!
??
OC底層原理三:探索alloc (你好谨娜,alloc大佬 )