前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS之objc源码编译报错解决方案(已提供编译好的源码)

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

作者头像
CC老师
发布2022-03-15 08:12:37
1.3K0
发布2022-03-15 08:12:37
举报

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

前言

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

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

【环境说明】

Mac电脑系统:macOS Big Sur 11.5.2

Xcode版本:13.2.1

官方objc源码下载:

opensource.apple.com/tarballs/ob…

我下载的是818.2版本

下载完并解压

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

先不管它们,直接运行。

1、unable to find sdk 'macosx.internal'

【解决方案】 Build Settings->搜Base SDK

Targets修改

Project修改

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

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

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

【解决方案】

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

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

下载地址:opensource.apple.com/tarballs/xn…

(可以参考格式,修改要下的版本号)

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

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

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

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

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

运行继续报错

【解决方案】 打开:

opensource.apple.com/tarballs/dy…

解压找到文件dyld_priv.h

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

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

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

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

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

解压找到文件:

libplatform-177.270.1/private/os/lock_private.h

根目录文件增加

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

找到文件:

libplatform-177.270.1/private/os/base_private.h

根目录文件增加

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

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

找到文件:

libpthread-416.100.3/private/tsd_private.h

根目录文件增加

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

打开网站(前面已经下载过了):

opensource.apple.com/tarballs/xn…

找到文件:

xnu-7195.81.3/osfmk/machine/cpu_capabilities.h

根目录文件增加

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

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

根目录文件增加

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

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

找到文件:

libpthread-416.100.3/private/spinlock_private.h

根目录文件增加

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

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

找到文件:

Libc-825.40.1/pthreads/pthread_machdep.h

根目录文件增加

12、'CrashReporterClient.h' file not found

找到文件:

Libc-825.40.1/include/CrashReporterClient.h

根目录文件增加

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

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

全局搜索pthread_lock_t

我们发现在spinlock_private.h和pthread_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

(滑动显示更多)

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

15、Use of undeclared identifier 'OS_UNFAIR_LOCK_ADAPTIVE_SPIN'

【解决方案】

// 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);

(滑动显示更多)

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

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

根目录文件增加

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

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

【解决方案】 将#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'

(滑动显示更多)

【解决方案】

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

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

21、Use of undeclared identifier 'dyld_platform_version_macOS_10_13'

【解决方案】

22、Use of undeclared identifier 'dyld_platform_version_macOS_10_11'

【解决方案】

23、Use of undeclared identifier 'dyld_fall_2018_os_versions'

【解决方案】

24、'_simple.h' file not found

找到文件:

libplatform-177.270.1/private/_simple.h

根目录文件增加

25、'Block_private.h' file not found

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

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

根目录文件增加

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

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

根目录文件增加

27、Use of undeclared identifier 'CRGetCrashLogMessage'

【解决方案】

Build Settings -> 搜索Preprocessor Macros, 添加

LIBC_NO_LIBCRASHREPORTERCLIENT

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

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

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

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

找到文件:

xnu-6153.81.5/osfmk/kern/restartable.h

根目录文件增加

30、Use of undeclared identifier 'oah_is_current_process_translated'

修改前

修改后

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

【解决方案】 注释这行代码

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

(滑动显示更多)

【解决方案】

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'

(滑动显示更多)

【解决方案】 注释选中代码

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

// 报错信息
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

35、library not found for -lCrashReporterClient

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

36、library not found for -loah

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

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

【解决方案】

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

【解决方案】TARGETS->Build Phases->Run Script(markgc)->修改macosx.internal为macosx

39、Expected function body after function declarator

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

终于编译成功了

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

新建一个调试

【点击+号】

【选择macOS - Command Line Tool】

【输入名称】

【关联依赖】

【运行ing】

报错在这一行

lock.lock();

错误信息提示是

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

(滑动显示更多)

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

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

【再次运行】

成功打印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文件夹结构图

编译好的源码

链接: pan.baidu.com/s/1xt2YuoZm…

提取码: ov85 --来自百度网盘超级会员v3的分享

来源:稀土掘金

作者:小青龙716

链接:https://juejin.cn/post/7049995719112917029

- END -

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-01-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 HelloCoder全栈小集 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档