一畏吓、下載objc4-756脊阴,編譯解決報(bào)錯(cuò)問(wèn)題
1、進(jìn)入源碼區(qū)亲雪,搜索objc4并下載 ?
https://opensource.apple.com/release/macos-10145.html
同時(shí)下載objc4相關(guān)依賴dyld跑揉、launchd锅睛、Libc埠巨、libauto、libclosure现拒、libdispatch辣垒、libplatform、libpthread印蔬、xnu勋桶。放在方便操作的文件夾,方便搜索查找侥猬。
2例驹、運(yùn)行工程編譯報(bào)錯(cuò)如下:
error: The i386 architecture is deprecated. You should update your ARCHS build setting to remove the i386 architecture. (in target ‘objc’)
error: The i386 architecture is deprecated. You should update your ARCHS build setting to remove the i386 architecture. (in target ‘objc-trampolines’)
解決方法:target-Build Settings -Architecture設(shè)置為Standard arcgutectures
3、解決問(wèn)題后編譯退唠,繼續(xù)報(bào)錯(cuò):
一看發(fā)現(xiàn)reason.h頭文件沒(méi)找到鹃锈,缺失依賴文件,在步驟1下載的文件中搜索sys/reason.h文件瞧预,加入即可屎债。我們需要?jiǎng)?chuàng)建一個(gè)include文件來(lái)存放這些缺失的依賴文件。
設(shè)置頭文件路徑:
搜索缺失文件:
添加頭文件后繼續(xù)編譯垢油,繼續(xù)添加缺失文件……
4盆驹、添加的過(guò)程中發(fā)現(xiàn)pthread_machdep.h在依賴中沒(méi)有,CrashReporterClient.h也沒(méi)有找到滩愁,也不知道在哪個(gè)庫(kù)躯喇,一臉懵逼,直接谷歌找到連接:
https://opensource.apple.com/source/Libc/Libc-825.24/pthreads/pthread_machdep.h
https://opensource.apple.com/source/Libc/Libc-825.26/include/CrashReporterClient.h
打開(kāi)連接復(fù)制源碼到新建文件惊楼,解決玖瘸,哭笑不得秸讹。最新的源碼居然缺少文件檀咙,還要在歷史版本庫(kù)找。
編譯后pthread_machdep.h文件報(bào)錯(cuò):
Typedef redefinition with different types (‘int’ vs ‘volatile OSSpinLock’ (aka 'volatile int’))
重復(fù)定義璃诀,注釋即可弧可。
編譯繼續(xù)報(bào)錯(cuò):
Static declaration of ‘_pthread_getspecific_direct’ follows non-static declaration
注釋掉pthread_machdep.h中所有的錯(cuò)誤信息。
5劣欢、編譯報(bào)錯(cuò):
Expected ','
extern bool dyld_program_minos_at_least(dyld_build_version_t version) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
直接注釋掉就行棕诵。繼續(xù)編譯,問(wèn)題繼續(xù)出現(xiàn)報(bào)錯(cuò):
Use of undeclared identifier 'DYLD_MACOSX_VERSION_10_14’
在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
問(wèn)題解決凿将。
6校套、接著步驟6編譯出現(xiàn):
’isa.h’ file not found
isa.h文件在工程runtime文件中復(fù)制粘貼到include文件下即可。
7牧抵、繼續(xù)編譯繼續(xù)報(bào)錯(cuò):’_simple.h’ file not found 搜索文件添加笛匙。
8侨把、編譯報(bào)錯(cuò):Use of undeclared identifier ‘CRGetCrashLogMessage’。
解決方法:target -> Build Settings -> Preprocessor Macros 添加LIBC_NO_LIBCRASHREPORTERCLIENT
9妹孙、編譯報(bào)錯(cuò):
clang? linker command failed with exit code 1 (use -v to see invocation)
no such file or directory: 'lCrashReporterClient’
解決方法:在 Build Settings -> Linking -> Other Linker Flags里刪掉"-lCrashReporterClient"(Debug和Release都刪了)
10秋柄、編譯報(bào)錯(cuò):
ld: can’t open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/AppleInternal/OrderFiles/libobjc.order
clang: error: linker command failed with exit code 1 (use -v to see invocation)
解決方法: Build Settings->Linking->Order File 改成$(SRCROOT)/libobjc.order
11、編譯報(bào)錯(cuò):
Showing All Messages
/xcodebuild? SDK “macosx.internal” cannot be located.
/xcrun? unable to find utility “clang++”, not a developer tool or in PATH
解決方法:將Target->Build Phases->Run-Script(markgc)里的內(nèi)容macosx.internal改為macosx
12蠢正、編譯報(bào)錯(cuò):
error: no such public header file: '/tmp/objc.dst/usr/include/objc/ObjectiveC.apinotes’
解決方法:Target->Build Settings->Other Text-Based InstallAPI Flags里的內(nèi)容設(shè)為空骇笔;
把Text-Based InstallAPI Verification Model里的值改為Errors Only。
13嚣崭、編譯報(bào)錯(cuò)笨触,額…… 居然編譯通過(guò)了。
二有鹿、添加test Target
1旭旭、添加一個(gè)target取名為?objc-test
2、引入頭文件#import <objc/message.h>創(chuàng)建一個(gè)新類HBObject葱跋,繼承自NSObject類持寄。添加屬性,方法娱俺,并給屬性賦值稍味,調(diào)用方法。運(yùn)行如下:
#import <Foundation/Foundation.h>
#import <objc/message.h>
void hb_test_method(Class cla, SEL _cmd){
? ? NSLog(@"我這個(gè)添加的方法被調(diào)用了");
}
int main(int argc, const char * argv[]) {
? ? @autoreleasepool {
? ? ? ? Class HBObject = objc_allocateClassPair(objc_getClass("NSObject"), "HBObject", 0);
? ? ? ? class_addIvar(HBObject, "name", sizeof(id), log2(sizeof(id)), @encode(id));
? ? ? ? class_addMethod(HBObject, sel_registerName("hb_test_method"), (IMP)hb_test_method, "v@:");
? ? ? ? objc_registerClassPair(HBObject);
? ? ? ? id newObject = [[HBObject alloc]init];
? ? ? ? [newObject setValue:@"yahibo" forKey:@"name"];
? ? ? ? NSLog(@"name:%@",[newObject valueForKey:@"name"]);
? ? ? ? objc_msgSend(newObject,sel_registerName("hb_test_method"));
? ? }
? ? return 0;
}
至此完成了源碼調(diào)試荠卷,編譯通過(guò)模庐。