问题
在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