一、下载objc4-756,编译解决报错问题
1、进入源码区,搜索objc4并下载
https://opensource.apple.com/release/macos-10145.html
同时下载objc4相关依赖dyld、launchd、Libc、libauto、libclosure、libdispatch、libplatform、libpthread、xnu。放在方便操作的文件夹,方便搜索查找。
2、运行工程编译报错如下:
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设置为Standard arcgutectures
3、解决问题后编译,继续报错:
一看发现reason.h头文件没找到,缺失依赖文件,在步骤1下载的文件中搜索sys/reason.h文件,加入即可。我们需要创建一个include文件来存放这些缺失的依赖文件。
设置头文件路径:
搜索缺失文件:
添加头文件后继续编译,继续添加缺失文件……
4、添加的过程中发现pthread_machdep.h在依赖中没有,CrashReporterClient.h也没有找到,也不知道在哪个库,一脸懵逼,直接谷歌找到连接:
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
打开连接复制源码到新建文件,解决,哭笑不得。最新的源码居然缺少文件,还要在历史版本库找。
编译后pthread_machdep.h文件报错:
Typedef redefinition with different types (‘int’ vs ‘volatile OSSpinLock’ (aka 'volatile int’))
重复定义,注释即可。
编译继续报错:
Static declaration of ‘_pthread_getspecific_direct’ follows non-static declaration
注释掉pthread_machdep.h中所有的错误信息。
5、编译报错:
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));
直接注释掉就行。继续编译,问题继续出现报错:
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
问题解决。
6、接着步骤6编译出现:
’isa.h’ file not found
isa.h文件在工程runtime文件中复制粘贴到include文件下即可。
7、继续编译继续报错:’_simple.h’ file not found 搜索文件添加。
8、编译报错:Use of undeclared identifier ‘CRGetCrashLogMessage’。
解决方法:target -> Build Settings -> Preprocessor Macros 添加LIBC_NO_LIBCRASHREPORTERCLIENT
9、编译报错:
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、编译报错:
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、编译报错:
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)里的内容macosx.internal改为macosx
12、编译报错:
error: no such public header file: '/tmp/objc.dst/usr/include/objc/ObjectiveC.apinotes’
解决方法:Target->Build Settings->Other Text-Based InstallAPI Flags里的内容设为空;
把Text-Based InstallAPI Verification Model里的值改为Errors Only。
13、编译报错,额…… 居然编译通过了。
二、添加test Target
1、添加一个target取名为 objc-test
2、引入头文件#import 创建一个新类HBObject,继承自NSObject类。添加属性,方法,并给属性赋值,调用方法。运行如下:
#import
#import
void hb_test_method(Class cla, SEL _cmd){
NSLog(@"我这个添加的方法被调用了");
}
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;
}
至此完成了源码调试,编译通过。