OC底层原理02:Objc4源码编译
简介
OC的一个主要特性Runtime运行时特性,在日常开发中我们使用到的都是不可见的,还好官方提供了 Runtime(objc4)
源码供开发者研究,我是 ZJ
,下面就开始迈出源码探索的第一步,源码编译。
下载objc4-781
- 进入源码区
打开源码链接
- 选择
macOS
,搜索objc4-
,然后下载到桌面
首先编译 target objc
- unable to find sdk ‘macosx.internal’
解决办法:
PROJECTS –> objc –> Build Settings, 将 Base SDK 改成 macOS
TARGETS –> objc –> Build Settings, 将 Base SDK 改成 macOS
- ‘sys/reason.h’ file not found
解决办法:
还是在 macOS
目录下,下载 xnu-8020.101.41
, 把 xnu-8020.101.41/bsd/sys/reason.h
文件复制到桌面创建的新文件夹 comm_ifl/sys
目录下
将文件 ZJCommon 放在工程根目录下,并在工程设置文件的索引路径
- target -> objc -> Build Settings -> Header Search Paths -> 添加
$(SRCROOT)/ZJCommon
注释错误 可以最后再弄
Unknown type name ‘uint32_t’
- 添加 #include <stdint.h>
‘mach-o/dyld_priv.h’ file not found
- 原理同上,下载
dyld-960
文件,将dyld_priv.h
文件,拷贝到comm_ifl/mach-o/dyld_priv.h
下
‘os/lock_private.h’ file not found
- 原理同上,下载
libplatform-273.100.5
文件,将libplatform-273.100.5/private/os/lock_private.h
中lock_private.h
文件,拷贝到comm_ifl/os/lock_private.h
下
extern dyld_platform_t dyld_get_active_platform(void) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0)); —- 报错 Expected ‘,’
由于调试主要基于
macos
,删除, bridgeos(3.0)
批量同上均删除
, bridgeos(3.0)
bridgeos(4.0) 问题
- 去除
, bridgeos(4.0)
‘os/base_private.h’ file not found
- 在
xnu-8020.101.41/libken/os/base_private.h
中, 把base_private.h
文件, 拷贝到comm_ifl/os/base_private.h
‘pthread/tsd_private.h’ file not found
- 下载
libpthread-libpthread-486.100.11
文件,将libpthread-libpthread-486.100.11/private/pthread/tsd_private.h
拷贝到comm_ifl/pthread/tsd_private.h
‘os/feature_private.h’ file not found
- 注释掉
‘os/variant_private.h’ file not found
- 拷贝
Libc-Libc-1507.100.9/os/variant_private.h
至comm_ifl/os/variant_private.h
‘os/tsd.h’ file not found
- 拷贝
xnu-xnu-8020.101.4/libsyscall/os/tsd.h
至comm_ifl/os/tsd.h
‘pthread/spinlock_private.h’ file not found
- 拷贝
libpthread-libpthread-486.100.11/private/pthread/spinlock_private.h
至comm_ifl/pthread/spinlock_private.h
Expected function body after function declarator
‘System/pthread_machdep.h’ file not found
‘System/pthread_machdep.h’文件只有在Libc-825.40.1及以下版本才有
- 在 https://opensource.apple.com/source/Libc/Libc-583/pthreads/ 上下载
pthread_machdep.h
,拷贝pthread_machdep.h
至comm_ifl/System/pthread_machdep.h
‘CrashReporterClient.h’ file not found
- 在 这里下载 拷贝
CrashReporterClient.h
至comm_ifl/CrashReporterClient.h
#include_next<CrashReporterClient.h> 改为
#include <CrashReporterClient.h>
依然报错:在 Build Settings -> Preprocessor Macros 中加入: LIBC_NO_LIBCRASHREPORTERCLIENT
Typedef redefinition with different types (‘int’ vs ‘volatile OSSpinLock’ (aka ‘volatile int’))
- 注释掉
Unexpected character <U+1F92F>
- 注释掉
Use of undeclared identifier ‘dyld_platform_version_macOS_10_13’
- 注释掉
‘os/reason_private.h’ file not found
- 拷贝
xnu-xnu-8020.101.4/libkern/os/reason_private.h
至comm_ifl/os/reason_private.h
‘os/bsd.h’ file not found
- 直接注释
‘objc-shared-cache.h’ file not found
- 拷贝
dyld-dyld-960/include/objc-shared-cache.h
至comm_ifl/objc-shared-cache.h
‘os/linker_set.h’ file not found
- 拷贝
Libc-Libc-1507.100.9/os/linker_set.h
至comm_ifl/os/linker_set.h
‘_simple.h’ file not found
- 全局搜索
_simple.h
拷贝至comm_ifl/_simple.h
Use of undeclared identifier ‘dyld_fall_2020_os_versions’
- 注释掉
Use of undeclared identifier ‘objc4’
- 注释掉
Use of undeclared identifier ‘preoptimizedCaches’
- 注释掉
Use of undeclared identifier ‘CRGetCrashLogMessage’
- 注释掉
‘Cambria/Traps.h’ file not found 和 ‘Cambria/Cambria.h’ file not found
- 注释掉
Use of undeclared identifier ‘objc_thread_get_rip’
‘kern/restartable.h’ file not found
xnu-xnu-8020.101.4/osfmk/kern/restartable.h
拷贝到comm_ifl/kern/restartable.h
Use of undeclared identifier ‘oah_is_current_process_translated’
- 注释掉
‘Block_private.h’ file not found
libclosure-libclosure-79.1/Block_private.h
拷贝到comm_ifl/Block_private.h
Use of undeclared identifier ‘os_parse_boot_arg_string’
- 注释掉
Use of undeclared identifier ‘dyld_platform_version_macOS_10_11’
- 注释掉
Use of undeclared identifier ‘dyld_fall_2018_os_versions’
Use of undeclared identifier ‘dyld_platform_version_macOS_10_12’
‘_static_assert’ declared as an array with a negative size
- 注释掉
最后这几步很重要
在 dyld_priv.h 文件顶部加入宏
1 | #define DYLD_MACOSX_VERSION_10_11 0x000A0B00 |
CrashReporterClient异常
library not found for -loah
‘macosx.internal’ cannot be located.
接下来objc进行编译调试
新建target :ZJDemo
建立依赖
创建一个macOS类ZJPerson
绑定到ZJDemo的target上
注释文件执行顺序
编译调试target
下符号调试
- Post title:OC底层原理02:Objc4源码编译
- Post author:张建
- Create time:2020-09-05 22:45:38
- Post link:https://redefine.ohevan.com/2020/09/05/OC底层原理/OC底层原理02:Objc4源码编译/
- Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.