objc源码编译


一、环境配置

  • macOS:12.5
  • Xcode:13.4.1
  • objc4-841.13

依赖项目

  • dyld-960
  • Libc-1507.100.9
  • libclosure-79.1
  • libplatform-273.100.5
  • libpthread-486.100.11
  • xnu-8020.140.41

所有的源码都可以在opensource中找到,最好找与当前系统匹配的版本下的文件。

二、配置

首先选中objc target进行编译。

  1. unable to find sdk 'macosx.internal'
    

    修改脚本 “macosx.internal” 为 “macosx” :

并且修改Base SdkmacOS

2.'sys/reason.h' file not found
在项目根目录创建自己的文件夹,我这里是 LibHeader,将xnu中的bsd/sys目录中的sys/reason.h文件拷贝到这个目录中:

img

配置Search Header

以同样的方式处理以下的:

错误 目录
'mach-o/dyld_priv.h' file not found dyld include/mach-o
'os/lock_private.h' file not found libplatform os/lock_private.h
'os/base_private.h' file not found xnu os/base_private.h
'pthread/tsd_private.h' file not found libpthread pthread/tsd_private.h
'System/machine/cpu_capabilities.h' file not found xnu System/machine/cpu_capabilities.hosfmk/machine/目录下的)
'os/tsd.h' file not found xnu os/tsd.h
'pthread/spinlock_private.h' file not found libpthread pthread/spinlock_private.h
'System/pthread_machdep.h' file not found libc System/pthread_machdep.hlibc新版本中没有找打,老版本有
'CrashReporterClient.h' file not found libc CrashReporterClient.h'libc新版本中没有找打,老版本有
'os/linker_set.h' file not found Libc os/linker_set.h
'Cambria/Traps.h' file not found xnu Cambria/Traps.h
'_simple.h' file not found libplatform _simple.h
'Cambria/Cambria.h' file not found 注释掉
'kern/restartable.h' file not found xnu kern/restartable.h
'os/feature_private.h' file not found 注释
objc-bp-assist.h' file not found 注释
'objc-shared-cache.h' file not found dyld objc-shared-cache.h
'os/feature_private.h' file not found 注释
'os/reason_private.h' file not found 注释
'os/variant_private.h' file not found 注释
'Block_private.h' file not found libclourse Block_private.h

还需要增加LIBC_NO_LIBCRASHREPORTERCLIENT宏定义:

3.Expected ','

注释掉bridgeos相关内容:

4.Use of undeclared identifier 'dyld_platform_version_macOS_10_11'


直接注释掉。

5.Use of undeclared identifier 'oah_is_current_process_translated'

直接注释

6.Use of undeclared identifier 'dyld_fall_2020_os_versions'

注释

7.Use of undeclared identifier 'objc4'


注释

8.'_static_assert' declared as an array with a negative size


注释

9.Use of undeclared identifier 'dyld_platform_version_macOS_10_13'


注释

10.Use of undeclared identifier 'dyld_fall_2018_os_versions'

修改

11.Use of undeclared identifier 'dyld_platform_version_bridgeOS_2_0'


注释

12.libobjc.order 路径问题
can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/AppleInternal/OrderFiles/libobjc.order

  1. library not found for -lCrashReporterClient

img

release 和 debug 都删除。

14.library not found for -loah


删除-loah

这个时候一般能正常编译通过了。

三、编译调试

  1. 新建一个macOS commandLine TargetObjcTest

    img

  2. 绑定二进制依赖关系

  3. 运行进行源码调试

    这个时候断点能够进入,就大功告成。

总结:
如果自己的Target编译调试遇见问题,尝试以下配置:
1: enable hardened runtime -> NO
2: build phase -> denpendenice -> objc
3: 如果main函数中断点断不住,尝试重新创建自己的Targetmain放在Compile Sources最前面。

可以直接下载我编译好的进行调试objc4-841.13 已编译好的源码