iOS - 使用clang将Object-C代码转换为C++代码时 'UIKit/UIKit.h' file not found

问题

在MacOS项目下使用clang命令clang -rewrite-objc main.m可以把main.m文件转换成C++文件,方便研究内部实现。但是如果是iOS项目的话执行上面clang命令就会报出下面错误:

shiyudeMacBook-Pro:LeaksTest shiyusun$ clang -rewrite-objc main.m
main.m:9:9: fatal error: 'UIKit/UIKit.h' file not found
#import <UIKit/UIKit.h>
        ^~~~~~~~~~~~~~~
1 error generated.

意思是找不到UIKit/UIKit.h文件。

解决办法

使用命令:

clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk main.m

如果不想每次都输入这么长的一串可以使用alias的方式:

进入终端,键入命令 vim

vim ~/.bash_profil

vim界面输入进入编辑编辑状态并且键入:

alias rewriteoc='clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk'

键入完毕,点esc退出编辑状态,再键入:wq退出vim并保存,执行source ~/.bash_profile这句一定要执行,执行才会生效。

最后使用别名命令试试看:

rewriteoc main.m

注意:当使用__weak修饰变量进行进行clang编译时会报错:

cannot create __weak reference because the current deployment target does
      not support weak references

这时需要支持ARC、指定运行时系统版本,输入命令:

xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-8.0.0 main.m
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值