iOS_objc4-756.2 最新源码编译调试

不少小伙伴在探索底层的时候常常很苦恼,只能靠猜想!稍微灵活一点的小伙伴会经过 Apple 提供的源码来辅助看一下,可是很显然这不太直观!若是可以直接编译调试,像咱们本身的代码直接 LLDB 调试,流程跟踪。那简直不要太爽,因而我编译了如今最新的iOS_objc4-756.2git

准备工做

若是想先来体验一把的小伙伴能够直接 GitHub 源码 下载!github

若是你也想亲自体验一把,请你准备条件:macos

  • mac OS 10.14
  • Xcode 10.1
  • objc4-756.2

以上资料均可以在 Apple source 获取到。api

开始配置

i386 架构问题

首先你下载的iOS_objc4-756.2 直接编译会报错以下:xcode

Showing Recent Messages The i386 architecture is deprecated. You should update your ARCHS build setting to remove the i386 architecture.架构

解决: 将 objcobjc-trampolines 中的 Build Settings 选项 Architectures 中的值切换为 Standard Architectures(64-bit Intel)app

文件查漏补缺

'sys/reason.h' file not foundide

这个资料我已经给你们找好了:学习

  • 你们能够经过 GitHub 源码 下载,路径在 objc-756.2编译资料/xnu-4903.241.1/bsd/sys/reason.h
  • 或者你们能够经过 Apple sourcexnu-4903.241.1/bsd/sys/reason.h 路径自行下载
  • 还能够经过谷歌中输入reason.h site:opensource.apple.com 定向检索

把找到的文件加入到工程里面。例如:ui

  • 我在根目录建立了一个 KCCommon 文件
  • 建立 sys 文件
  • reason.h 文件加入进去

目前还不行,必定给咱们的工程设置文件检索路径

  • 选择 target -> objc -> Build Settings
  • 在工程的 Header Serach Paths 中添加搜索路径 $(SRCROOT)/KCCommon

'mach-o/dyld_priv.h' file not found

'os/lock_private.h' file not found

'os/base_private.h' file not found

'pthread/tsd_private.h' file not found

'System/machine/cpu_capabilities.h' file not found

'os/tsd.h' file not found

'pthread/spinlock_private.h' file not found

'System/pthread_machdep.h' file not found

'CrashReporterClient.h' file not found

'objc-shared-cache.h' file not found

'_simple.h' file not found

'Block_private.h' file not found

上面的报错状况处理方式都是和 'sys/reason.h' file not found 同样的解决

文件补漏状况以下图:

CrashReporterClient异常

咱们若是直接导入 Apple source 下载的 CrashReporterClient 仍是会报错:

'CrashReporterClient.h' file not found

解决:

  • 须要在 Build Settings -> Preprocessor Macros 中加入:LIBC_NO_LIBCRASHREPORTERCLIENT
  • 或者下载我给你们的文件CrashReporterClient,这里面咱们直接更改了里面的宏信息 #define LIBC_NO_LIBCRASHREPORTERCLIENT

dyld_priv 文件修改

GitHub 源码 这是修改过的!下面板书我修改了什么

报错:Use of undeclared identifier ‘DYLD_MACOSX_VERSION_10_13

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
复制代码

libobjc.order 路径问题

Can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/AppleInternal/OrderFiles/libobjc.order

  • 选择 target -> objc -> Build Settings
  • 在工程的 Order File 中添加搜索路径 $(SRCROOT)/libobjc.order

lCrashReporterClient 编译不到

Library not found for -lCrashReporterClient

  • 选择 target -> objc -> Build Settings
  • Other Linker Flags 中删除 -lCrashReporterClient ( DebugRelease 都删了)

_objc_opt_class 没法编译

Undefined symbol: _objc_opt_class

这个问题是由于要适配新系统:MacOS 10.15, 由于如今笔者写这一篇文章的时候,尚未正式版本推送!这里咱们向下兼容 MacOS 10.14

Xcode 脚本编译问题

/xcodebuild:1:1: SDK "macosx.internal" cannot be located.

/xcrun:1:1: sh -c '/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -sdk macosx.internal -find clang++ 2> /dev/null' failed with exit code 16384: (null) (errno=No such file or directory)

/xcrun:1:1: unable to find utility "clang++", not a developer tool or in PATH

  • 选择 target -> objc -> Build Phases -> Run Script(markgc)
  • 把脚本文本 macosx.internal 改为 macosx

ObjectiveC.apinotes 异常

no such public header file: '/tmp/objc.dst/usr/include/objc/ObjectiveC.apinotes'

  • 选择 target -> objc -> Build Settings
  • Text-Based InstallAPI Verification Model 中添加搜索路径 Errors Only

  • Other Text-Based InstallAPI Flags 清空全部内容

编译成功

接下来你能够编译 - Build Succeeded - 恭喜你!💐

可能到这里你已经老血吐了一地了!的确配置过程仍是相对来讲有点恶心,尤为是文件的查漏补缺,可是咱们享受编译成功的喜悦吧!

objc 编译调试

  • 新建一个 Target : LGTest

  • 绑定二进制依赖关系

  • 运行代码进入源码,你们能够自由编译调试咯!

总结

iOS 如今更多的会偏向底层开发研究,可调式编译的 objc4 源码可以帮助更快速学习和更容易理解!博客持续更新中,谢谢你们的关注点赞!Thanks♪(・ω・)ノ

相关文章
相关标签/搜索