首先以main.m为例:
1、进入对应文件目录
cd ...
2、执行clang -rewrite...
clang -rewrite-objc main.m
执行完这句可能会出现如下错误提示:
![](https://img.haomeiwen.com/i1787970/686f94cd7b73fd8a.png)
3、如何解决这个问题:
解决方法
clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.1.sdk main.m
对于这个路径呢,你可以先复制
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/ 去finder中查看具体的文件名字。
![](https://img.haomeiwen.com/i1787970/43c562527863e5b7.png)
如图我的的文件夹是:iPhoneSimulator.sdk所以把上述的iPhoneSimulator9.1.sdk改成iPhoneSimulator.sdk
即:
clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk main.m
这样就可以看到同目录下生成一个main.cpp的文件。
![](https://img.haomeiwen.com/i1787970/315fcc33f34b840f.png)
完美吗? 不不,不完美,每次的命令行太长了 ,费神。
4、参考:Objective-C编译成C++代码报错重新设置指令的别名:
vim ~/.bash_profile
进入编辑状态——>输入:
alias rewriteoc='clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk'
rewriteoc这个别名嘛,您自己起个就成。
最后执行:
source ~/.bash_profile
![](https://img.haomeiwen.com/i1787970/182f240b115b96f8.png)
![](https://img.haomeiwen.com/i1787970/899c163efa1a849c.png)
这次真的完美了。
网友评论