1. 程式人生 > >使用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

轉換成功

補充一下

指定真機

1
xcrun -sdk iphoneos clang -rewrite-objc ViewController.m

指定模擬器

1
xcrun -sdk iphonesimulator clang -rewrite-objc ViewController.m

指定 SDK 版本

1
xcrun -sdk iphonesimulator10.3 clang -rewrite-objc ViewController.m