使用clang将OC代码转为C++(亲测有效)

【使用clang将OC代码转为C++(亲测有效)】使用clang将OC代码转为C++(亲测有效)
文章图片

最近在研究RunLoop运行机制,想看看他的底层代码是什么样子的,于是百度看看大家如何操作的。最终绝望的发现基本的操作都一样,然并卵。

zhangquaniMac:3456 zq$ cd /Users/zq/Desktop/3456/3456 zhangquaniMac:3456 zq$ clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk ViewController.m

显示异常如下:
clang: warning: using sysroot for 'iPhoneSimulator' but targeting 'MacOSX' [-Wincompatible-sysroot] warning: include path for stdlibc++ headers not found; pass '-std=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found] 1 warning generated. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:12856:188: warning: 'kCFCalendarUnitWeek' is deprecated: first deprecated in macOS 10.10 - Use kCFCalendarUnitWeekOfYear or kCFCalendarUnitWeekOfMonth instead [-Wdeprecated-declarations] ...or NSCalendarUnitWeekOfYear, depending on which you mean"))) = kCFCalend... ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:5156:2: note: 'kCFCalendarUnitWeek' has been explicitly marked deprecated here kCFCalendarUnitWeek __attribute__((availability(macos,introduced=10.4,d... ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22487:16: warning: 'ns_returns_retained' attribute only applies to functions that return an Objective-C object [-Wignored-attributes] __attribute__((__ns_returns_retained__)) _Nullable sec_trust_t ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22493:16: warning: 'ns_returns_retained' attribute only applies to functions that return an Objective-C object [-Wignored-attributes] __attribute__((__ns_returns_retained__)) _Nullable sec_identity_t ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22496:16: warning: 'ns_returns_retained' attribute only applies to functions that return an Objective-C object [-Wignored-attributes] __attribute__((__ns_returns_retained__)) _Nullable sec_identity_t ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22505:16: warning: 'ns_returns_retained' attribute only applies to functions that return an Objective-C object [-Wignored-attributes] __attribute__((__ns_returns_retained__)) _Nullable sec_certificate_t ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22521:16: warning: 'ns_returns_retained' attribute only applies to functions that return an Objective-C object [-Wignored-attributes] __attribute__((__ns_returns_retained__)) _Nullable dispatch_data_t ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22555:16: warning: 'ns_returns_retained' attribute only applies to functions that return an Objective-C object [-Wignored-attributes] __attribute__((__ns_returns_retained__)) _Nullable dispatch_data_t ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22559:16: warning: 'ns_returns_retained' attribute only applies to functions that return an Objective-C object [-Wignored-attributes] __attribute__((__ns_returns_retained__)) _Nullable dispatch_data_t ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:50310:13: warning: 'CADisplayLink' is only available on macOS 10.14 or newer [-Wunguarded-availability-new] - (nullable CADisplayLink *)displayLinkWithTarget:(id)target selector:(S... ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:33690:12: note: 'CADisplayLink' has been explicitly marked partial here @interface CADisplayLink : NSObject ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:50310:1: note: annotate 'displayLinkWithTarget:selector:' with an availability attribute to silence this warning - (nullable CADisplayLink *)displayLinkWithTarget:(id)target selector:(S... ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52754:18: error: 'UIWebView' is unavailable: not available on macOS - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRe... ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52702:281: note: 'UIWebView' has been explicitly marked unavailable here ...__attribute__((availability(macos,unavailable))) @interface UIWebView : ... ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52755:30: error: 'UIWebView' is unavailable: not available on macOS - (void)webViewDidStartLoad:(UIWebView *)webView __attribute__((availabi... ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52702:281: note: 'UIWebView' has been explicitly marked unavailable here ...__attribute__((availability(macos,unavailable))) @interface UIWebView : ... ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52756:31: error: 'UIWebView' is unavailable: not available on macOS - (void)webViewDidFinishLoad:(UIWebView *)webView __attribute__((availab... ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52702:281: note: 'UIWebView' has been explicitly marked unavailable here ...__attribute__((availability(macos,unavailable))) @interface UIWebView : ... ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52757:18: error: 'UIWebView' is unavailable: not available on macOS - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)err... ^ /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52702:281: note: 'UIWebView' has been explicitly marked unavailable here ...__attribute__((availability(macos,unavailable))) @interface UIWebView : ... ^ 9 warnings and 4 errors generated.

试了多次,发现还是报同样的错误,于是只能继续耐心的看看还有没有不同的操作,最终得到了一个完美的操作:
zhangquaniMac:~ zq$ cd /Users/zq/Desktop/3456/3456 zhangquaniMac:3456 zq$ xcrun -sdk iphonesimulator clang -rewrite-objc ViewController.m

转换成功
使用clang将OC代码转为C++(亲测有效)
文章图片

补充一下
指定真机
xcrun -sdk iphoneos clang -rewrite-objc ViewController.m

指定模拟器
xcrun -sdk iphonesimulator clang -rewrite-objc ViewController.m

指定SDK版本
xcrun -sdk iphonesimulator10.3 clang -rewrite-objc ViewController.m

?语言转C ++
zhangquaniMac:~ zq$ cd /Users/zq/Desktop/Test/Test zhangquaniMac:Test zq$ clang -rewrite-objc main.c


参考:HTTPS://cotin.tech/iOS/clang-rewrite-objc/

    推荐阅读