编译环境
macOS Monterey (12.3.1)Xcode 13.2.1 (13C100)
编译问题
1、unable to find sdk 'macosx.internal'
PROJECT -> objc -> Build Settings -> Base SDK设置的macosx.internal更改成macOS
2.
Targets -> objc -> Build Phases -> Run Script(markgc)下的Shall脚本中的macosx.internal更改成macosx
2、sys/reason.h' file not found
- objc源码根目录下新建
common/文件夹,并创建子文件夹sys. Targets -> objc -> Build Setting -> Header Search Paths配置common路径- 下载xnu-8019.80.24源码,在
/bsd/sys/目录下找到reason.h文件 - 将
reason.h复制到自己创建的sys/文件夹下
3、'mach-o/dyld_priv.h' file not found
*common目录下创建子文件夹mach-o
- 下载dyld-941.5源码,在
dyld-941.5/include/mach-o/目录下找到dyld_priv.h文件 - 将
dyld_priv.h复制粘贴到自己创建的common/mach-o/目录下
3.1、dyld_priv.h文件中报错Expected ','
- 删除
dyld_priv.h文件中所有的报错处的bridgeos(3.0)
4、'os/lock_private.h' file not found
common目录下创建子文件夹os- libplatform-273.40.1源码,在
/private/os/目录下找到lock_private.h文件 - 将
lock_private.h复制粘贴到自己创建的common/os/目录下
4.1、lock_private.h文件中报错'os/base_private.h' file not found
- 该文件位于
xnu-8019.80.24/libkern/os/目录下 - 将
base_private.h文件复制粘贴到自己创建的common/os/目录下
4.2、lock_private.h文件中报错Expected ','
- 同上删除文件中报错位置的
bridgeos(4.0)即可
5、'pthread/tsd_private.h' file not found
common/目录下创建子文件夹pthread- libpthread-485.60.2源码,在
libpthread/private/pthread/目录下找到tsd_private.h文件 - 将
tsd_private.h文件复制粘贴到自己创建的common/pthread/目录下
6、'System/machine/cpu_capabilities.h' file not found
common目录下新建子文件夹System,然后在System目录下在创建子文件夹machine- 该文件位于
xnu-8019.80.24/osfmk/machine/目录下 - 将
cpu_capabilities.h文件复制粘贴到自己创建的common/System/目录下
7、'os/tsd.h' file not found
- 该文件位于
xnu-8019.80.24/libsyscall/os/目录下 - 将
tsd.h文件复制粘贴到自己创建的common/os/目录下
8、'pthread/spinlock_private.h' file not found
- 该文件位于
libpthread-485.60.2/private/pthread/目录下 - 讲
spinlock_private.h文件复制粘贴到自己创建的common/pthread/目录下
9、'System/pthread_machdep.h' file not found
- 下载Libc-825.40.1源码,在
pthreads/目录下找到pthread_machdep.h文件 - 将
pthread_machdep.h文件复制粘贴到自己创建的common/System/目录下
10、'CrashReporterClient.h' file not found
解决方案如下:
- 找到
CrashReporterClient.h文件
- 该文件位于
Libc-825.40.1/include/目录下 - 将
CrashReporterClient.h文件复制粘贴到common/目录下
- 在
CrashReporterClient.h文件中添加宏定义
#define LIBC_NO_LIBCRASHREPORTERCLIENT
- 在
Targers -> objc -> Build Settings -> Preprocessor Macros中添加LIBC_NO_LIBCRASHREPORTERCLIENT
- 在
Targers -> objc -> Build Settings -> Other Linker Flags -> Debug中 删除-lCrashReporterClient
11、pthread_machdep.h中四处报错如下图:
解决方案: 将报错代码全部注释掉即可
12、'os/feature_private.h' file not found
- 直接注释掉该头文件
13、 Use of undeclared identifier 'dyld_fall_2020_os_versions'
- 注释掉报错相关的两行代码
14、Use of undeclared identifier 'dyld_platform_version_macOS_10_13'
- 注释掉报错代码块
15、Use of undeclared identifier 'objc4'
- 注释掉报错代码块
16、Use of undeclared identifier 'dyld_platform_version_macOS_10_11'
- 注释掉
&& !dyld_program_sdk_at_least(dyld_platform_version_macOS_10_11)条件判断
17、'objc-shared-cache.h' file not found
- 该文件位于
dyld-941.5/include/目录下 - 将
objc-shared-cache.h文件复制粘贴到common/目录下
18、Use of undeclared identifier 'dyld_fall_2018_os_versions'
- 注释掉
&& dyld_program_sdk_at_least(dyld_fall_2018_os_versions)条件判断
19、'_simple.h' file not found
- 该文件位于
libplatform-273.40.1/private/目录下 - 将
_simple.h文件复制粘贴到自己创建的common/目录下
20、'os/linker_set.h' file not found
- 该文件位于
xnu-8019.80.24/bsd/sys/目录下 - 讲
linker_set.h文件复制粘贴到自己创建的common/os/目录下
21、'Cambria/Traps.h' file not found
- 注释掉下图两行代码
22、'Block_private.h' file not found
- 下载libclosure-79源码,在其目录下找到
Block_private.h文件 - 将
Block_private.h文件复制粘贴到自己创建的common/目录下
23、'kern/restartable.h' file not found
- 该文件位于
xnu-8019.80.24/osfmk/kern/目录下 *common/目录下新建子文件夹kern - 将
restartable.h文件复制粘贴到自己创建的common/kern/目录下
24、objc-cache.mm文件报错
Use of undeclared identifier 'oah_is_current_process_translated'与Use of undeclared identifier 'objc_thread_get_rip'报错- 注释掉下图代码块
25、NSObject.mm文件报错
'os/feature_private.h' file not found
- 注释掉该头文件即可
'os/reason_private.h' file not found
- 该文件位于
xnu-8019.80.24/libkern/os/目录下, - 将
reason_private.h文件复制粘贴到自己创建的common/os/目录下
3.'os/variant_private.h' file not found
- 该文件位于
Libc-1506.40.4/os/目录下 - 将
variant_private.h文件复制粘贴到自己创建的common/os/目录下
se of undeclared identifier 'dyld_platform_version_bridgeOS_2_0'
- 注释掉该代码块即可
26、variant_private.h 文件中4处报Expected ','问题
- 同上面的处理方法一样 删掉
bridgeos参数即可
27、'_static_assert' declared as an array with a negative size
- 注释掉截图两行即可
28、library not found for -loah
Targers -> objc -> Build Settings -> Other Linker Flags -> Debug中 删除-loah
Build Succeeded
- 到此为止 objc源码终于编译成功
编译调试
- 新建一个
macOS commandLine Target,AJObjcBuild
- 绑定二进制依赖关系以及添加依赖库
- 编译
AJObjcBuild
- 此时编译完成 断点也可以成功进入
源码断点问题
- 在objc内部源码打断点时发现断点不走
- 在
Targers -> AJObjcBuild -> Build Settings -> Singing -> Enable Hardened Runtime中的值设置为NO即可
- 大功告成 断点成功断到我们想要的地方
遇到的问题
- 刚开始编译时 其他都可以在修改
Enable Hardened Runtime为NO时一直崩溃,后续升级系统版本为macOC Monterey才可以正常调试, 所以objc-838.1需要在macOC Monterey运行
Demo
- 最后附上编译好的objc4-838.1Demo
- 希望能对底层探索的你有所帮助