由于電腦還沒有升級到最新版本,所以本文章沒有用當(dāng)前最新源碼,進(jìn)行調(diào)試。只是選取了macOS 10.15。其實調(diào)試內(nèi)容基本一樣,除個別報錯外。
準(zhǔn)備工作
- mac OS 10.15
- Xcode 12.4
- objc4-779.1


在 Apple source 獲取到。
報錯解決
錯誤一:運行源碼后,會提示2個同樣錯誤,系統(tǒng)要求的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 讓系統(tǒng)給你進(jìn)行匹配

錯誤二:文件查漏補缺
'sys/reason.h' file not found

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

- 我在根目錄創(chuàng)建了一個
common文件 - 創(chuàng)建
sys文件 - 把
reason.h文件加入進(jìn)去
解決一.png
目前還不行,一定給我們的工程設(shè)置文件檢索路徑
- 選擇
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異常
如果直接導(dǎo)入 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源碼工程就能編譯成功了,完整的工程文件如下:

參考:
http://www.itdecent.cn/p/bffba01690d7
http://www.itdecent.cn/p/5809940f77d9
https://juejin.cn/post/6844904082226806792
