编译objc4-838.1

447 阅读5分钟

1.编译objc4-838.1

1.1 编译环境

image.png

image.png

1.2 编译遇到的问题

1.2.1 unable to find sdk "macosx.internal"

解决办法如下: 将PROJECTTARGETS里面Build Settings中的Base SDK改为macOS。

image.png

1.2.2 ’xxx.h‘ file not found

先来一个找不到文件的索引

找不到的文件文件位置
os/feature_private.hxxxx.h
sys/reason.hbsd/sys/.h
mach-o/dyld_priv.hdyld/include/mach-o/dyld_priv.h
os/lock_private.hlibplatform/private/include/os/lock_private.h
os/base_private.hxnu/libkern/os/base_private.h
pthread/tsd_private.hlibpthread/private/pthread/tsd_private.h
System/machine/cpu_capabilities.hxnu/osfmk/machine/cpu_capabilities.h
os/tsd.hxnu/libsyscall/os/tsd.h
pthread/spinlock_private.hlibpthread/private/spinlock_private.h
System/pthread_machdep.hlibc/pthreads/pthread_machdep.h
CrashReporterClient.hlibc/include/CrashReporterClient.h
objc-shared-cache.hdyld/include/objc-shared-cache.h
Block_private.hlibclosure-79/Block_Private/Block_private.h
os/reason_private.hxnu/libkern/os/reason_private.h
os/variant_private.hlibc/os/variant_private.h
_simple.hlibplatform/private/_simple.h
os/linker_set.hxnu/bsd/sys/linker_set.h
Cambria/Traps.hxxxx.h
kern/restartable.hxnu/osfmk/kern/restartable.h

注意上面图表中xxxx.h文件是苹果没有公布出来,不用费尽心思去找这些文件.

1.2.2.1 'os/feature_private.h' file not found

解决办法如下: 将引用feature_private.h的地方注释掉,即注释掉objc-runtime.mm中的36行与444~446行

//    if (!os_feature_enabled_simple(objc4, preoptimizedCaches, true)) {
//        DisablePreoptCaches = true;
//    }

1.2.2.2 'sys/reason.h' file not found

该文件在xnu-8019.80.24文件的/bsd/sys/目录下,下载xnu代码,在objc源码目录下建立一个include文件夹,再建立一个sys目录,将reason.h文件放入,再前往objc的Build Settings设置Header Search Paths如以下图示:

image.png

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

1.该文件位于dyld/include/mach-o/目录下面,下载dyld源码,在上面建立的include文件夹中新建mach-o文件夹,并且将dyld_priv.h文件复制粘贴进去。

image.png 2.dyld_priv.h 报错语法错误 Expected ',' 解决方案就是将报Expected ','错误的全部注释

1.2.2.4 'os/lock_private.h' file not found

该文件位于 libplatform/private/include/os/ 目录下,同上下载libplatform源码.在include文件夹中建立os文件夹并且将lock_private.h文件复制粘贴进去。

image.png

1.2.2.5 'os/base_private.h' file not found

该文件同样位于 xnu/libkern/os/  目录下,同上将base_private.h文件复制粘贴进入os文件夹中即可。

image.png

1.2.2.6 'pthread/tsd_private.h' file not found

该文件位于libpthread/private/pthread  目录下,下载libpthread源码,在include文件夹中新建pthread文件夹并且将tsd_private.h文件复制粘贴进去.

image.png

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

该文件位于 xnu/osfmk/machine/ 目录下,在include文件夹中新建System文件夹,再System文件夹中新建machine文件夹,并且将tsd_private.h文件复制粘贴进machine文件夹去.

image.png

1.2.2.8 'os/tsd.h' file not found

该文件位于xnu/libsyscall/os/  目录中,同上复制粘贴文件到os文件夹中。

1.2.2.9 'pthread/spinlock_private.h' file not found

该文件位于 libpthread/private/目录中,同上复制粘贴文件到pthread文件夹中。

image.png

1.2.2.10 'System/pthread_machdep.h' file not found

这个文件位于 libc/pthreads/目录下,同上下载libc源码复制粘贴文件到System文件夹中即可,如果最新的版本中没有,可以前往旧版本中寻找,Libc-583版本中有该文件。

1.2.2.11 'CrashReporterClient.h' file not found

解决方案如下:

1.该文件位于 libc/include/ 目录下,在Libc-Libc-1506.40.4版本中没有CrashReportClient.h文件。我找了一个Libc-997.90.3版本的,将CrashReporterClient.h复制粘贴到include文件中。

2.在target -> Build Setting -> Preprocessor Macros中加入LIBC_NO_LIBCRASHREPORTERCLIENT或者在CrashReporterClient.h文件中更改了里面的宏信息 #define LIBC_NO_LIBCRASHREPORTERCLIENT

3.在Build Settings -> Linking -> Other Linker Flags里删掉"-lCrashReporterClient",因为后面运行的时候会报错ld: library not found for -lCrashReporterClient

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

该文件位于dyld/include目录下,将objc-shared-cache.h文件复制粘贴到include文件夹中。

1.2.2.13 'Block_private.h' file not found

该文件位于libclosure-79/Block_Private中,下载libclosure-79源码,将Block_private.h文件拷贝到include文件夹中。

1.2.2.14 'os/reason_private.h' file not found

该文件位于xnu/libkern/os/目录中,将reason_private.h文件复制粘贴到include/os文件夹中。

1.2.2.15 'os/variant_private.h' file not found

该文件位于libc/os/目录中,将variant_private.h文件复制粘贴到include/os文件夹中。

1.2.2.16 '_simple.h' file not found

该文件位于libplatform/private/目录中,将_simple.h文件复制粘贴到include文件夹中。

1.2.2.17 'os/linker_set.h' file not found

该文件位于xnu/bsd/sys目录中,将linker_set.h文件复制粘贴到include/os文件夹中。

1.2.2.18 ‘Cambria/Traps.h’ file not found

注释掉下面的代码

image.png

image.png

1.2.2.19 'kern/restartable.h' file not found

该文件位于xnu/osfmk/kern目录中,将restartable.h文件复制粘贴到include/kern文件夹中。

1.2.3 Use of undeclared identifier 'OS_UNFAIR_LOCK_ADAPTIVE_SPIN'

image.png 将上面代码改为os_unfair_lock_lock(&mLock);

1.2.4 Use of undeclared identifier 'dyld_platform_version_macOS_10_13'

解决方案如下图:

image.png

1.2.5 Mismatch in debug-ness macros

注释报错地方的代码:

#error mismatch in debug-ness macros

1.2.6 Use of undeclared identifier 'dyld_fall_2020_os_versions'

注释掉下面代码

image.png

1.2.7 '_static_assert' declared as an array with a negative size

直接注释掉报错的地方 image.png

1.2.8 Use of undeclared identifier ‘dyld_fall_2018_os_versions’

删掉dyld_fall_2018_os_versions判断

image.png

1.2.9 ld: library not found for -loah

Build Settings -> Linking -> Other Linker Flags里删掉"-loah"

1.2.10 SDK "macosx.internal" cannot be located.

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

1.2.11 error: headers don't have symbol xxxxx

error: headers don't have symbol '_AltHandlerDebugLock'
error: headers don't have symbol '_AssociationsManagerLock'
error: headers don't have symbol '_CppObjectLocks'
error: headers don't have symbol '_DebugAltHandlers'
error: headers don't have symbol '_DebugDontCrash'
error: headers don't have symbol '_DebugDuplicateClasses'
error: headers don't have symbol '_DebugFragileSuperclasses'
error: headers don't have symbol '_DebugMissingPools'
error: headers don't have symbol '_DebugNilSync'
error: headers don't have symbol '_DebugNonFragileIvars'
error: headers don't have symbol '_DebugPoolAllocation'
error: headers don't have symbol '_DebugUnload'
error: headers don't have symbol '_DemangleCacheLock'
error: headers don't have symbol '_DisableInitializeForkSafety'
error: headers don't have symbol '_DisableNonpointerIsa'
error: headers don't have symbol '_DisablePreopt'
error: headers don't have symbol '_DisableTaggedPointerObfuscation'
error: headers don't have symbol '_DisableTaggedPointers'
error: headers don't have symbol '_DisableVtables'
error: headers don't have symbol '_FirstHeader'
error: headers don't have symbol '_LastHeader'
error: headers don't have symbol '_MultithreadedForkChild'
error: headers don't have symbol '_NXMapKeyCopyingInsert'
error: headers don't have symbol '_NXMapKeyFreeingRemove'
.....

解决方案:

  • 选择 target -> objc -> Build Settings

  • Text-Based InstallAPI Verification Model 中添加搜索路径 Errors Only

  • Other Text-Based InstallAPI Flags 清空所有内容

2.编译调试

1.新建一个macOS commandLine TargetObjc4Test

image.png

2.绑定二进制依赖关系以及添加库

image.png

3.运行进行源码调试

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

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