objc4-750源码编译

本文详细记录了Objective-C Runtime(objc4)源码的编译过程,包括解决各种编译错误和缺失依赖的问题,以及如何成功添加Debug Target。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

OC的一个主要特性Runtime运行时特性,在日常开发中我们使用到的都是不可见的,还好官方提供了Runtimeobjc4)源码供开发者研究,我是小白,下面就开始迈出源码探索的第一步,源码编译。

源码地址

一、下载objc4-750,编译解决报错问题

1、进入源码区,搜索objc4并下载

[外链图片转存失败(img-i3TF9tqM-1562592897578)(https://upload-images.jianshu.io/upload_images/1609369-73b28a28a0e73002.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

同时下载objc4相关依赖dyld、launchd、Libc、libauto、libclosure、libdispatch、libplatform、libpthread、xnu。放在方便操作的文件夹,方便搜索查找。

dependent.png

2、下载后解压,并打开工程,如下图:

project.png

3、运行工程编译报错如下:

[外链图片转存失败(img-nX5wcveR-1562592897580)(https://upload-images.jianshu.io/upload_images/1609369-5381ad817f4ad0b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

报错信息:
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

architectures.png

4、解决问题后编译,继续报错:

reasonerror.png

一看发现reason.h头文件没找到,缺失依赖文件,在步骤1下载的文件中搜索sys/reason.h文件,加入即可。我们需要创建一个include文件来存放这些缺失的依赖文件。
设置头文件路径:

header.png

搜索缺失文件:

find.png

添加头文件后继续编译,继续添加缺失文件……

5、添加的过程中发现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中所有的错误信息。

6、编译报错:
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

问题解决。

7、接着步骤6编译出现:
’isa.h’ file not found
isa.h文件在工程runtime文件中复制粘贴到include文件下即可。

isa.png

8、继续编译继续报错:’_simple.h’ file not found 搜索文件添加。
9、编译报错:Use of undeclared identifier ‘CRGetCrashLogMessage’
解决方法:target -> Build Settings -> Preprocessor Macros 添加LIBC_NO_LIBCRASHREPORTERCLIENT

preprocessor.png

10、编译报错:
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"DebugRelease都删了)

ICrashReporter.png

11、编译报错:
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

libobjc.png

12、编译报错:
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

macosx.png

13、编译报错:
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

ObjectiveC.apinotes.png

14、编译报错,额…… 居然编译通过了。

二、添加Debug Target

1、添加一个target取名为 objc-test

objctest.png

2、引入头文件#import <objc/message.h>创建一个新类HBObject,继承自NSObject类。添加属性,方法,并给属性赋值,调用方法。运行如下:

objc_test.png

代码:

#import <Foundation/Foundation.h>
#import <objc/message.h>

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;
}

至此完成了源码调试,编译通过。

注意如果报错:Undefined symbol: _objc_opt_class,需要适配Mac系统,project -> deployment target Mac OS选择10.14

调试成功的源码地址

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值