objc4-750源码编译

news/2024/7/20 21:25:41 标签: iOS, 源码, runtime

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

调试成功的源码地址


http://www.niftyadmin.cn/n/1737833.html

相关文章

解读 nginx 中 chain 和 buf

在写 nginx 的 filter 模块时候需要比较深入去了解chain 和 buf的一些细节以及数据流处理过程。 就结构而言&#xff0c;这两个结构不算复杂 struct ngx_chain_s { ngx_buf_t *buf; ngx_chain_t *next;}; struct ngx_buf_s { u_char *pos; u_char …

GCD部分总结

一、概述 多线程任务管理&#xff0c;基于C语言的底层API&#xff0c;采用闭包形式与外界通讯&#xff0c;代码简洁高效&#xff1b;充分利用多核CPU&#xff0c;自动管理线程的生命周期&#xff0c;我们只负责任务的创建。 二、队列和任务 1、队列 常用的数据结构之一&…

理解JAVASCRIPT 中hasOwnProperty()的作用

【转载】http://hj149.javaeye.com/blog/505237 hasOwnProperty&#xff1a;是用来判断一个对象是否有你给出名称的属性或对象。不过需要注意的是&#xff0c;此方法无法检查该对象的原型链中是否具有该属性&#xff0c;该属性必须是对象本身的一个成员。格式如下&#xff1a; …

iOS图片处理

一、图片大小 二、强制解压缩 三、灰度图片 四、图片调色 五、设置马赛克 一、图片大小 位图&#xff1a;由一个个像素点组成的图像 图片像素点个数&#xff1a;就是图片宽高乘积 一个像素点的大小&#xff1a;4个字节&#xff08;存放RGBA值&#xff0c;每一分量占1个字节&…

站点SEO优化

一&#xff0e; 选好关键字。关键字是搜索引擎找到我们的网站的关键。所以我们一定要先给自己的网站设定好一个或几个关键字。关键字不易太多&#xff0c;以5-10个为宜。选择关键字前可以先查询一下和你网站相关的关键字热度排行&#xff0c;选取热度比较高的关键字。 二&#…

简易图片选择器

主要使用PhotoKit框架包含了与photos相关的文件。一下为常用的几个类&#xff1a; 1、PHPhotoLibrary phphotolibrary提供对用户照片库中照片、视频和相关内容的元数据和图像数据的访问&#xff0c;包括来自相机卷、iCloud共享、照片流、导入和从iTunes同步的内容。 获取当前…

HTML的head区

HTML的head区是指首页HTML代码的<head>和</head>之间的内容。 一&#xff0c;公司版权注释 <!---inc corp name, time---> 二&#xff0c;网页显示字符集 如&#xff1a; 简体中文&#xff1a;<META HTTP-EQUIV"Content-Type" CONTENT&…

HTML中的meta

from:http://baike.baidu.com/view/953191.htm meta标签  meta是html语言head区的一个辅助性标签。几乎所有的网页里&#xff0c;我们可以看到类似 下面这段的html代码&#xff1a; &#xff1c;head&#xff1e; &#xff1c;meta http-equiv"content-Type" …