-
生成
- 使用clang编译器将Objective-C代码编译成C语言代码, 并生成在一个.cpp的 C++文件中:
$ cd 当前文件夹
$ clang -rewrite-objc ViewController.m
- 可能会遇到一个错误:

- 解决办法是将之前执行的命令替换为:
$ clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk ViewController.m
-
优化
-
一、这个命令很繁琐不易记, 可以用alias起一个别名来代替该命令。
-
1.打开终端, 键入命令
vim ~/.bash_profile
-
2.在vim界面输入i进入编辑编辑状态并且键入:
rewriteoc
(自己起的
alias rewriteoc='clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk'
-
3.键入完毕后,点击Esc退出编辑,再按 Shift+: ,最后再按:wq保存并退出vim文件并保存
-
4.键入命令
source ~/.bash_profile
(必须要执行这句)
-
-
二、优化后的使用
$ cd 当前文件夹
$ rewriteoc ViewController.m
- 解释:优化后也就是起别名,
rewriteoc ViewController.m
上下两句等同
$ clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk ViewController.m
- 结果


- 补充:如果vim编辑器出现
E325: ATTENTION
,终端执行下面的代码就会好了
rm -f ~/.bash_profile.swp
-
三、遇到下面问题后的处理办法 __weak问题解决
- 在使用clang转换OC为C++代码时,可能会遇到以下问题
cannot create __weak reference in file using manual reference
- 解决方案:支持ARC、指定运行时系统版本,比如,在-rewrite-objc 后面添加
-fobjc-arc -fobjc-runtime=ios-8.0.0
出现问题前如下:
$ clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk ViewController.m
添加完 -fobjc-arc -fobjc-runtime=ios-8.0.0
如下
$ clang -x objective-c -rewrite-objc -fobjc-arc -fobjc-runtime=ios-8.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk ViewController.m
网友评论