iOS拓展---[转载]使用clang将OC代码转为C++(亲测有效)

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

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

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

显示异常如下:

  1. clang: warning: using sysroot for 'iPhoneSimulator' but targeting 'MacOSX' [-Wincompatible-sysroot]
  2. warning: include path for stdlibc++ headers not found; pass '-std=libc++' on the
  3. command line to use the libc++ standard library instead
  4. [-Wstdlibcxx-not-found]
  5. 1 warning generated.
  6. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:12856:188: warning:
  7. 'kCFCalendarUnitWeek' is deprecated: first deprecated in macOS 10.10 - Use
  8. kCFCalendarUnitWeekOfYear or kCFCalendarUnitWeekOfMonth instead
  9. [-Wdeprecated-declarations]
  10. ...or NSCalendarUnitWeekOfYear, depending on which you mean"))) = kCFCalend...
  11. ^
  12. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:5156:2: note:
  13. 'kCFCalendarUnitWeek' has been explicitly marked deprecated here
  14. kCFCalendarUnitWeek __attribute__((availability(macos,introduced=10.4,d...
  15. ^
  16. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22487:16: warning:
  17. 'ns_returns_retained' attribute only applies to functions that return an
  18. Objective-C object [-Wignored-attributes]
  19. __attribute__((__ns_returns_retained__)) _Nullable sec_trust_t
  20. ^
  21. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22493:16: warning:
  22. 'ns_returns_retained' attribute only applies to functions that return an
  23. Objective-C object [-Wignored-attributes]
  24. __attribute__((__ns_returns_retained__)) _Nullable sec_identity_t
  25. ^
  26. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22496:16: warning:
  27. 'ns_returns_retained' attribute only applies to functions that return an
  28. Objective-C object [-Wignored-attributes]
  29. __attribute__((__ns_returns_retained__)) _Nullable sec_identity_t
  30. ^
  31. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22505:16: warning:
  32. 'ns_returns_retained' attribute only applies to functions that return an
  33. Objective-C object [-Wignored-attributes]
  34. __attribute__((__ns_returns_retained__)) _Nullable sec_certificate_t
  35. ^
  36. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22521:16: warning:
  37. 'ns_returns_retained' attribute only applies to functions that return an
  38. Objective-C object [-Wignored-attributes]
  39. __attribute__((__ns_returns_retained__)) _Nullable dispatch_data_t
  40. ^
  41. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22555:16: warning:
  42. 'ns_returns_retained' attribute only applies to functions that return an
  43. Objective-C object [-Wignored-attributes]
  44. __attribute__((__ns_returns_retained__)) _Nullable dispatch_data_t
  45. ^
  46. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:22559:16: warning:
  47. 'ns_returns_retained' attribute only applies to functions that return an
  48. Objective-C object [-Wignored-attributes]
  49. __attribute__((__ns_returns_retained__)) _Nullable dispatch_data_t
  50. ^
  51. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:50310:13: warning:
  52. 'CADisplayLink' is only available on macOS 10.14 or newer
  53. [-Wunguarded-availability-new]
  54. - (nullable CADisplayLink *)displayLinkWithTarget:(id)target selector:(S...
  55. ^
  56. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:33690:12: note:
  57. 'CADisplayLink' has been explicitly marked partial here
  58. @interface CADisplayLink : NSObject
  59. ^
  60. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:50310:1: note:
  61. annotate 'displayLinkWithTarget:selector:' with an availability attribute
  62. to silence this warning
  63. - (nullable CADisplayLink *)displayLinkWithTarget:(id)target selector:(S...
  64. ^
  65. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52754:18: error:
  66. 'UIWebView' is unavailable: not available on macOS
  67. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRe...
  68. ^
  69. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52702:281: note:
  70. 'UIWebView' has been explicitly marked unavailable here
  71. ...__attribute__((availability(macos,unavailable))) @interface UIWebView : ...
  72. ^
  73. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52755:30: error:
  74. 'UIWebView' is unavailable: not available on macOS
  75. - (void)webViewDidStartLoad:(UIWebView *)webView __attribute__((availabi...
  76. ^
  77. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52702:281: note:
  78. 'UIWebView' has been explicitly marked unavailable here
  79. ...__attribute__((availability(macos,unavailable))) @interface UIWebView : ...
  80. ^
  81. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52756:31: error:
  82. 'UIWebView' is unavailable: not available on macOS
  83. - (void)webViewDidFinishLoad:(UIWebView *)webView __attribute__((availab...
  84. ^
  85. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52702:281: note:
  86. 'UIWebView' has been explicitly marked unavailable here
  87. ...__attribute__((availability(macos,unavailable))) @interface UIWebView : ...
  88. ^
  89. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52757:18: error:
  90. 'UIWebView' is unavailable: not available on macOS
  91. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)err...
  92. ^
  93. /var/folders/qc/wbfmgr1x6f9246c0jt6yqt100000gn/T/ViewController-4ce726.mi:52702:281: note:
  94. 'UIWebView' has been explicitly marked unavailable here
  95. ...__attribute__((availability(macos,unavailable))) @interface UIWebView : ...
  96. ^
  97. 9 warnings and 4 errors generated.

试了多次,发现还是报同样的错误,于是只能继续耐心的看看还有没有不同的操作,最终得到了一个完美的操作:

  1. zhangquaniMac:~ zq$ cd /Users/zq/Desktop/3456/3456
  2. zhangquaniMac:3456 zq$ xcrun -sdk iphonesimulator clang -rewrite-objc ViewController.m

转换成功

补充一下

指定真机

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 ++

  1. zhangquaniMac:~ zq$ cd /Users/zq/Desktop/Test/Test
  2. zhangquaniMac:Test zq$ clang -rewrite-objc main.c


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

T

原文地址:https://www.cnblogs.com/1-434/p/10584681.html