環(huán)境
- macOS 10.14.1
- Xcode 10.3
- objc4-750.1
下載源碼以及相關(guān)依賴
- 在MacOS 10.14.1 Source下載相關(guān)源碼,
Command + F快捷鍵查找objc4,下載。
1.png - 下載
objc4相關(guān)依賴庫,dyld、libauto、Libc、libclosure、libdispatch、libplatform、libpthread、xnu等,解壓后的文件夾如下圖:
2.png
針對編譯錯(cuò)誤一一修改
1、出錯(cuò)原因:
error:The i386 architecture is deprecated. You should update your ARCHS build setting to remove the i386 architecture. (in target 'objc-trampolines')
error: The i386 architecture is deprecated. You should update your ARCHS build setting to remove the i386 architecture. (in target 'objc')
解決方案: 找出target(objc和objc-trampolines都要改) -> Build Settings -> Architectures ->選擇標(biāo)準(zhǔn)架構(gòu)。

2、在objc-os.h頭文件里找不到sys/reason.h文件
解決:工程目錄下創(chuàng)建Common/sys目錄,在編譯設(shè)置(Build
Settings里面搜索,Header Search Paths,然后將Common索
引添加進(jìn)去),然后在之前下載的依賴包中搜索reason.h(路
徑:xnu-4903.221.2/bsd/sys/reason.h)頭文件,復(fù)制到
Common/sys目錄下,如圖:

3、在objc-os.h頭文件報(bào)錯(cuò)mach-o/dyld_priv.h file not found
解決:在Common文件夾下添加mach-o文件夾,在依賴庫文件中搜索dyld_priv.h,路徑為dyld-635.2/include/mach-o/dyld_priv.h,搜索到的文件添加進(jìn)mac-o文件夾內(nèi)

4、'os/lock_private.h' file not found
依賴庫文件路徑:libplatform-177.200.16/private/os/lock_private.h,在Common文件夾下創(chuàng)建os文件夾,將該頭文件添加進(jìn)去。
5、總結(jié)缺失的文件,按照上述方法添加
'os/base_private.h' file not found,在libplatform-177.200.16/private/os/base_private.h'pthread/tsd_private.h' file not found,在libpthread-330.220.2/private/tsd_private.hSystem/machine/cpu_capabilities.h' file not found,在xnu-4903.221.2/osfmk/machine/cpu_capabilities.h'os/tsd.h' file not found,在xnu-4903.221.2/libsyscall/os/tsd.h'pthread/spinlock_private.h' file not found,在libpthread-330.220.2/private/spinlock_private'System/pthread_machdep.h' file not found,在Libc-825.26/pthreads/pthread_machdep.h,(PS:這個(gè)頭文件在Libc-1272.200.26下找不到哦,請下載Libc-825.26)'CrashReporterClient.h' file not found,在Libc-825.26/include/CrashReporterClient.h,并且在Build Settings->Preprocessor Macros中加入:LIBC_NO_LIBCRASHREPORTERCLIENT'Block_private.h' file not found,在libdispatch-1008.220.2/src/BlocksRuntime/Block_private.h'objc-shared-cache.h' file not found,在dyld-635.2/include/objc-shared-cache.hTypedef redefinition with different types ('int' vs 'volatile OSSpinLock' (aka 'volatile int')),這種redefinition錯(cuò)誤時(shí),在工程目錄下搜索pthread_lock_t,發(fā)現(xiàn)有兩個(gè)文件夾里都定義了pthread_lock_t,注釋掉pthread_machdep.h文件中的定義提示
Static declaration of '_pthread_getspecific_direct' follows non-static declaration,這里三個(gè)函數(shù)定義重復(fù),這里把pthread_machdep.h文件中的定義注釋掉。'_simple.h' file not found,在libplatform-177.200.16/private/_simple.h'isa.h' file not found,isa.h文件在項(xiàng)目runtime文件夾中,把它引入到common文件夾下can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/AppleInternal/OrderFiles/libobjc.order,修改工程配置,將Build Settings->Linking->Order File改為工程根目錄下的libobjc.order,即:$(SRCROOT)/libobjc.order。library not found for -lCrashReporterClient,此時(shí)在 Build Settings -> Linking -> Other Linker Flags里刪掉"-lCrashReporterClient"(Debug和Release都刪了)SDK "macosx.internal" cannot be located. xcrun: error: unable to find utility "clang++", not a developer tool or in PATH,把Target-objc的Build Phases->Run Script(markgc)里的內(nèi)容macosx.internal改為macosxno such public header file: '/tmp/objc.dst/usr/include/objc/ObjectiveC.apinotes',這里需要把Target-objc的Build Settings->Other Text-Based InstallAPI Flags里的內(nèi)容設(shè)為空!并且一定記得要把Text-Based InstallAPI Verification Model里的值改為Errors Only.
完整的項(xiàng)目目錄結(jié)構(gòu)如下圖:

最終編譯成功~
測試代碼
新建一個(gè)Target,在MyMac上我創(chuàng)建了一個(gè)命令行的工程。
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
Class newClass = objc_allocateClassPair(objc_getClass("NSObject"), "newClass",
0);
objc_registerClassPair(newClass);
id newObject = [[newClass alloc] init];
NSLog(@"%@",newObject);
}
return 0;
}

