由于想了解OC通过runtime机制转成的C++代码是什么样子的,在进入项目目录中,执行cc -rewrite-objc ViewController.m
时如下错:
In file included from ViewController.m:9:
**./ViewController.h:9:9: ****fatal error: ****'UIKit/UIKit.h' file not found**
#import <UIKit/UIKit.h>
** ^**
1 error generated.
![](https://i-blog.csdnimg.cn/blog_migrate/028c0fd4bc29ad213914451709039c8b.webp?x-image-process=image/format,png)
fuck
遂在Google上找到各种类似的解决办法,但都没有对症下药,无论尝试那种办法都报‘UIKit/UIKit.h’ file not found的错。
但最终还是摸索出解决办法,就是使用:
clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk xxxxx.m
当然,不能每次都输入这么长的路径和命令,可通过alias简化工作,步骤:
- 进入终端,键入命令
vim ~/.bash_profile
- 在vim界面输入
i
进入编辑编辑状态并且键入: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
<-这句一定要执行,执行才会生效
图例:
![](https://i-blog.csdnimg.cn/blog_migrate/cce3159d83bc7d46cf4917b5bff5893d.webp?x-image-process=image/format,png)
alias建别名
![](https://i-blog.csdnimg.cn/blog_migrate/95c7541f8e11bba69b346a030c309500.webp?x-image-process=image/format,png)
运行~/.bash_profile
然后进入项目目录,找到你想要编译的文件,执行rewriteoc xxx.m
即可成功,图例
![](https://i-blog.csdnimg.cn/blog_migrate/ac14fefd4c93e0463b4c8b84331a33d6.webp?x-image-process=image/format,png)
激动又紧张
![](https://i-blog.csdnimg.cn/blog_migrate/57ff35429fd102a773c5dd0d3513960d.webp?x-image-process=image/format,png)
成功
最后感受下这浓浓的情谊:
![](https://i-blog.csdnimg.cn/blog_migrate/881fcb3e6012edb905695537aed3a738.webp?x-image-process=image/format,png)