iOS之objc源码编译报错解决方案(已提供编译好的源码)

2,916 阅读2分钟

和谐学习!不急不躁!!我是你们的老朋友小青龙~

前言

今天分享一篇关于官方objc源码工程编译错误的解决方案。

说来惭愧,之前的很多发文一直都是用的Cooci老师弄好的源码库,作为一个爱学习的程序员(呸,臭不要脸),应当学会自己开车(是去幼儿园的那种),自己解决问题。

  • 编译好的源码在文章末尾,可自行下载

环境说明

  • Mac电脑系统:macOS Big Sur 11.5.2
  • Xcode版本:13.2.1

官方objc源码下载opensource.apple.com/tarballs/ob…

我下载的是818.2版本

image.png

下载完并解压

image.png

打开objc4-818.2工程,我们发现了几个缺失文件:

image.png

image.png

先不管它们,直接运行。

1、unable to find sdk 'macosx.internal'

image.png

解决方案Build Settings->搜Base SDK

Targets修改

image.png

Project修改

image.png

如果改完发现还是报这个错误,记得clean一下再运行。

2、'sys/reason.h' file not found

image.png

运行发现再次报错,原因是缺失reason.h这个文件。

解决方案

打开网站:opensource.apple.com/source/xnu/

复制最新版本名(我这里是xnu-7195.81.3

image.png

下载地址opensource.apple.com/tarballs/xn… (可以参考格式,修改要下的版本号)

下载完会得到一个文件:xnu-7195.81.3.tar

解压,找到reason.h (xnu-7195.81.3 -> bsd -> sys -> reason.h)

image.png

在项目根目录创建文件结构lostFiles -> sys -> reason.h

image.png

Targets -> Build Setting -> Header Search Paths->添加$(SRCROOT)/lostFiles

image.png

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

运行继续报错

image.png

解决方案】 打开:opensource.apple.com/tarballs/dy…

image.png

解压找到文件dyld_priv.h

image.png

lostFiles下创建一个文件夹mach-o,并拖动dyld_priv.h到文件夹mach-o

image.png

4、dyld_priv.h文件报错Expected ','

image.png

解决方案】把这行代码注释了就行。

image.png

5、'os/lock_private.h' file not found

image.png

打开网站:opensource.apple.com/tarballs/li…

image.png

解压找到文件:libplatform-177.270.1/private/os/lock_private.h

根目录文件增加

image.png

6、'os/base_private.h' file not found

找到文件:libplatform-177.270.1/private/os/base_private.h

根目录文件增加

image.png

7、'pthread/tsd_private.h' file not found

image.png

打开网站:opensource.apple.com/tarballs/li…

image.png

找到文件:libpthread-416.100.3/private/tsd_private.h

根目录文件增加

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

image.png

打开网站(前面已经下载过了):opensource.apple.com/tarballs/xn…

image.png

找到文件:xnu-7195.81.3/osfmk/machine/cpu_capabilities.h

根目录文件增加

image.png

9、'os/tsd.h' file not found

image.png

找到文件:xnu-7195.81.3/libsyscall/os/tsd.h

根目录文件增加

image.png

10、'pthread/spinlock_private.h' file not found

libpthread-416.100.3库前面已经下载过了)

找到文件:libpthread-416.100.3/private/spinlock_private.h

根目录文件增加

image.png

11、'System/pthread_machdep.h' file not found

image.png

打开网站:opensource.apple.com/tarballs/Li…

image.png

找到文件:Libc-825.40.1/pthreads/pthread_machdep.h

image.png

根目录文件增加

image.png

12、'CrashReporterClient.h' file not found

找到文件:Libc-825.40.1/include/CrashReporterClient.h

image.png

根目录文件增加

image.png

13、Typedef redefinition with different types ('int' vs 'volatile OSSpinLock' (aka 'volatile int'))

image.png

字面意思是 使用不同类型重新定义了pthread_lock_t

全局搜索pthread_lock_t

image.png

我们发现在spinlock_private.hpthread_machdep.h,都对pthread_lock_t进行了定义。

解决方案】 注释pthread_machdep.h里的pthread_lock_t定义这行代码。

14、多个declaration错误

// 错误信息如下

Static declaration of '_pthread_has_direct_tsd' follows non-static declaration

Static declaration of '_pthread_getspecific_direct' follows non-static declaration

Static declaration of '_pthread_setspecific_direct' follows non-static declaration

image.png

解决方案】把这三个函数连带实现内容都注释掉

image.png

15、Use of undeclared identifier 'OS_UNFAIR_LOCK_ADAPTIVE_SPIN'

image.png

解决方案

// uint32_t opts = OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION | OS_UNFAIR_LOCK_ADAPTIVE_SPIN;

// 修改为
uint32_t opts = OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION;
os_unfair_lock_lock(&mLock);

image.png

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

image.png

找到文件:dyld-852.2/include/objc-shared-cache.h

image.png

根目录文件增加

image.png

17、'objc-probes.h' file not found

image.png

18、'os/feature_private.h' file not found

image.png

解决方案】 将#include <os/feature_private.h>这行注释掉

19、找不到未申明的'dyld_fall_2020_os_versions'、'objc4'、'preoptimizedCaches'

// 报错信息

Use of undeclared identifier 'dyld_fall_2020_os_versions'

Use of undeclared identifier 'objc4'

Use of undeclared identifier 'preoptimizedCaches'

解决方案

image.png

image.png

20、'objc-bp-assist.h' file not found

解决方案】 注释这行引用代码

image.png

21、Use of undeclared identifier 'dyld_platform_version_macOS_10_13'

解决方案

image.png

22、Use of undeclared identifier 'dyld_platform_version_macOS_10_11'

解决方案

image.png

23、Use of undeclared identifier 'dyld_fall_2018_os_versions'

解决方案

image.png

24、'_simple.h' file not found

image.png

找到文件:libplatform-177.270.1/private/_simple.h

根目录文件增加

image.png

25、'Block_private.h' file not found

打开网站:opensource.apple.com/tarballs/li…

image.png

解压并找到文件:libclosure-79/Block_private.h

根目录文件增加

image.png

26、'os/linker_set.h' file not found

找到文件:Libc-1439.40.11/os/linker_set.h

根目录文件增加

image.png

27、Use of undeclared identifier 'CRGetCrashLogMessage'

解决方案

Build Settings -> 搜索Preprocessor Macros, 添加LIBC_NO_LIBCRASHREPORTERCLIENT

image.png

添加完再编译,就不报这个错了

image.png

28、'Cambria/Traps.h' file not found

image.png

解决方案】为了避免提示'Cambria/Cambria.h' file not found,干脆两行都注释了

image.png

29、'kern/restartable.h' file not found

找到文件:xnu-6153.81.5/osfmk/kern/restartable.h

image.png

根目录文件增加

image.png

30、Use of undeclared identifier 'oah_is_current_process_translated'

修改前

image.png

修改后

image.png

31、'os/feature_private.h' file not found

解决方案】 注释这行代码

image.png

32、'os/reason_private.h' file not found | 'os/variant_private.h' file not found

// 报错信息

'os/reason_private.h' file not found

'os/variant_private.h' file not found

解决方案

image.png

33、Use of undeclared identifier 'dyld_platform_version_xx_x_x'

// 报错信息

Use of undeclared identifier 'dyld_platform_version_bridgeOS_2_0'

Use of undeclared identifier 'dyld_platform_version_iOS_10_0'

Use of undeclared identifier 'dyld_platform_version_macOS_10_12'

Use of undeclared identifier 'dyld_platform_version_tvOS_10_0'

Use of undeclared identifier 'dyld_platform_version_watchOS_3_0'

解决方案】 注释选中代码

image.png

34、Can't open order file: /Applications/Xcode.app/Contents…OrderFiles/libobjc.order

image.png

// 报错信息
Can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/AppleInternal/OrderFiles/libobjc.order

解决方案Build Settings->搜orderfile->添加$(SRCROOT)/libobjc.order

image.png

35、library not found for -lCrashReporterClient

image.png

解决方案Build Settings->搜other link->删除-lCrashReporterClient

image.png

36、library not found for -loah

image.png

解决方案】跟35一样,Build Settings->搜other link->删除-loah

37、'_static_assert' declared as an array with a negative size

解决方案

image.png

38、SDK "macosx.internal" cannot be located.

解决方案TARGETS->Build Phases->Run Script(markgc)->修改macosx.internalmacosx

image.png

39、Expected function body after function declarator

image.png

很多地方报了一样的错误提示Expected function body after function declarator

终于编译成功了

经过一系列问题的修复,终于看到了久违了Build Successded(😭)

image.png

新建一个调试

【点击+号】

image.png

【选择macOS - Command Line Tool

image.png

【输入名称】

image.png

【关联依赖】

image.png

【运行ing】

IMB_i2u7My.GIF

报错在这一行

lock.lock();

错误信息提示是

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

错误提示看不到什么有用的提示,但是我们知道肯定是lock方法出了问题。

解决方案】注释lock方法里的os_unfair_lock_lock(&mLock);这行代码

image.png

【再次运行】

IMB_XofkCl.GIF

成功打印NSLog日志。

参考文章链接

lock方法错误解决

感谢这位小伙伴提供的Demo,我用demo里面的objc-os.h文件和我自己工程的objc-os.h文件对比了下,发现lock方法不太一样,最终才意识到是多了一行代码。

objc4源码编译与调试
www.jianshu.com/p/bffba0169…

编译错误解决

参考文章:

iOS-objc4-781源码编译配置流程
www.jianshu.com/p/4f15c551a…

objc源码编译
www.jianshu.com/p/5fc26fed9…

由于是在Xcode13.2.1版本上编译的,所以在配置一些缺失的库文件上,我尽可能的选择最新的。

附一张lostFiles文件夹结构图

image.png

编译好的源码

链接: pan.baidu.com/s/1xt2YuoZm…
提取码: ov85 --来自百度网盘超级会员v3的分享