cpp 转换 fatal error: 'UIKit/UIKit.h' file not found

$ clang -rewrite-objc main.m  这样操作会报

解决方案一:

fatal error: 'UIKit/UIKit.h' file not found
#import <UIKit/UIKit.h>
        ^~~~~~~~~~~~~~~
1 error generated.

解决

将之前执行的命令替换成为:

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

然后, 你会发现, 你的文件夹中多了一个.cpp的文件, 证明解决了这个问题。

拓展

如果你觉得这个命令很繁琐不易记, 你可以采用 alias来起一个别名来代替这个命令。 
1.打开终端, 键入命令 vim ~/.bash_profile
2.在vim界面输入i进入编辑编辑状态并且键入: 

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

3.键入完毕后, esc退出编辑状态, 再键入:wq退出vim并保存 
4.键入命令source ~/.bash_profile

参考资料: 
iOS: XCode 4.5: xcodebuild error - fatal error: ‘UIKit/UIKit.h’ file not found

全文地址请点击:https://blog.csdn.net/wangyanchang21/article/details/79608668?utm_source=copy 

https://blog.csdn.net/majiakun1/article/details/52842010

==================================================================================

解决方案2: (简单) 以main.m 为例

第一步: cd 到需要转换的文件夹(一定要cd到要转换类所在的文件).      /Users/youxin/Desktop/BlockTest/BlockTest

第二部 :

     a : 如果是模拟器 :$  xcrun -sdk iphonesimulator clang -rewrite-objc main.m

     b:真机 : $ xcrun -sdk iphoneos clang -rewrite-objc main.m

    c : 真机 +模拟器 有默认版本的 :$  xcrun -sdk iphonesimulator9.3 clang -rewrite-objc main.m

clang -rewrite-objc的作用是把oc代码转写成c/c++代码,我们常用它来窥探OC的一些秘密。 

//下面是原文和链接:

1、最简单的例子

main.m的代码如下: 

打开终端,来到main.m所在目录,执行如下命令: 
执行之后,目录下多出一个main.cpp文件,打开一看,一两百行代码。其关键代码如下: 

enter image description here

这个例子是用来研究block的原理的,在网上能搜到,本文无意解释这些代码。 

2、指定SDK

有的oc代码要转成c代码时,在真机和模拟器上差别还是挺大的。如果需要指定SDK,那么要结合xcrun命令,例如指定真机: 

如果要指定模拟器: 

模拟器和真机都有默认的SDK版本,也可以指定具体某版本: 

当然,你要先看看你机器上都装哪些SDK,xcodebuild命令可以完成这个任务:

2.png

3、指定framework

如果使用了第三SDK,例如(main.m文件): 

代码中使用了听云的SDK,这时运行clang -rewrite-objc ,结果如下: 

3.png

出错了,忘记引入听云的这个framework了,clang不知到何处去找,这时需要用-F开关告诉它。 

执行命令,OK,成功了。 

以上示例都是为了尽可能简单,实际上可以同时指定多个源文件、多个framework。 

分享即快乐。 

clang--rewrite-objc

原文链接:http://www.ituring.com.cn/article/217522

猜你喜欢

转载自blog.csdn.net/yst19910702/article/details/82970729