1. 程式人生 > >使用Concorde庫在真機上因bitcode編譯出錯的解決方法

使用Concorde庫在真機上因bitcode編譯出錯的解決方法

    xcode7.3 新開iOS專案,使用Cocoapods管理第三方庫。

    Concorde 是 一種漸進的載入方式,它可以讓圖片 以更好看的方式加載出來。

    模擬器編譯執行成功,上真機時報錯:

    libturbojpeg.a does not contain bitcode. You must rebuild it with bitcode enabled (Xcodesetting ENABLE_BITCODE), obtain an updated library from the vendor, or disablebitcode for this target. for architecture arm64.

    查找了文章,說專案預設設定了bitcode為YES,則引入的第三方的庫也要設定bitcode為YES。如果第三方庫不支援bitcode,則專案設定bitcode為NO就可以解決這個問題。

    這樣做確實解決了問題,能編譯通過了,但是仔細察看一下同時引入的其它庫,都是設定bitcode為YES,因為這個就修改專案設定,非常可惜。

    再查詢libturbojpeg 的官方資料,上面寫明瞭支援bitcode的版本是1.5.1 :

    libjpeg-turbo 1.5.1 and later:
    iOS 6.0 and later / ARMv7 w/ bitcode (iPhone 3GS-4S, iPad 1st-3rd generation, iPad Mini)
    iOS 6.0 and later / ARMv7s w/ bitcode (iPhone 5 & 5C, iPad 4th generation)
    iOS 7.0 and later / ARMv8 w/ bitcode (iPhone 5S and later, iPad Mini 2 and later, iPad Air)

    去github的libjpeg-turbo主頁去查詢1.5.1版本,可惜沒找到.a檔案,又一番baidu之後找到這篇文章:使用OpenVC的一些報錯及解決方法,內容貼在下面。

    安裝到系統後,把libturbojpeg.a 檔案拷貝到專案檔案裡,bitcode設定為YES,成功編譯並執行在真機裡。

    以後更新cocoapods可能還需要複製一次,我覺得還是值得的,畢竟bitcode是蘋果推的設定,有利於app的下載。

用Xcode 7 beta 3在真機(iOS 8.3)上執行一下工程,結果發現工程編譯不過。看了下問題,報的是以下錯誤:

ld: ‘/Users/**/Framework/SDKs/PolymerPay/Library/mobStat/lib**SDK.a(**ForSDK.o)’does not contain bitcode. You must rebuild it with bitcode enabled (Xcodesetting ENABLE_BITCODE), obtain an updated library from the vendor, or disablebitcode for this target. for architecture arm64

得到的資訊是引入的一個第三方庫不包含bitcode。

Bitcode是個什麼鬼?

查閱了一下官方文件,在App

Distribution Guide–App Thinning (iOS, watchOS)一節中,找到了下面這樣一個定義:

Bitcode is an intermediate representationof a compiled program. Apps you upload to iTunes Connect that contain bitcodewill be compiled and linked on the App Store. Including bitcode will allowApple to re-optimize your app binary in the future without the need to submit anew version of your app to the store.

說的是bitcode是被編譯程式的一種中間形式的程式碼。包含bitcode配置的程式將會在App store上被編譯和連結。bitcode允許蘋果在後期重新優化程式的二進位制檔案,而不需要重新提交一個新的版本到App store上。

而在What’s New in Xcode-New Features in Xcode 7中,還有一段如下的描述:

Bitcode. When you archive for submission tothe App Store, Xcode will compile your app into an intermediate representation.The App Store will then compile the bitcode down into the 64 or 32 bitexecutables as necessary.

當提交程式到App store上時,Xcode會將程式編譯為一箇中間表現形式(bitcode)。然後App store會再將這個botcode編譯為可執行的64位或32位程式。

再看看這兩段描述,都是放在App Thinning(App瘦身)一節中,可以看出其與包的優化有關了。

Bitcode配置

在上面的錯誤提示中,提到了如何處理我們遇到的問題:

You must rebuild it with bitcode enabled(Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, ordisable bitcode for this target. for architecture arm64

要麼讓第三方庫支援,要麼關閉target的bitcode選項。

實際上,在Xcode 7中,我們新建一個iOS程式時,bitcode選項預設是設定為YES的。我們可以在”Build Settings”->”Enable Bitcode”選項中看到這個設定。不過,我們現在需要考慮的是三個平臺:iOS,Mac OS,watchOS。

對於iOS,bitcode是可選的;對於watchOS,bitcode是必須的;而Mac OS是不支援bitcode。

如果我們開啟了bitcode,在提交包時,下面這個介面也會有個bitcode選項:

所以,如果我們的工程需要支援bitcode,則必要要求所有引入的第三方庫都支援bitcode。

通過本文對bitcode的概念及配置情況的簡要介紹,希望iOS開發人員在工程執行中遇到類似的情況,可以根據上文的介紹更有效的找到原因並及時處理。



1.報錯:

Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for architecture 'armv7'


螢幕快照 2016-10-27 下午2.24.26.png

2.報錯:

"_jpeg_free_small", referenced from:

a.到http://sourceforge.net/projects/libjpeg-turbo/files/1.4.0/ 下載ibjpeg-turbo-1.4.0.dmg(3.8 MB)檔案。
b.安裝,一旦完成可以在該路徑下找到已經安裝的檔案 /opt/libjpeg-turbo/lib
c.開啟終端輸入命令 lipo -info /opt/libjpeg-turbo/lib/libjpeg.a
d.如果終端顯示以下命令執行後輸出,就意味著,你的專案(armv7/armv7s) * Architectures in the fat file: /opt/libjpeg-turbo/lib/libturbojpeg.a are: i386 x86_64 armv6 armv7 armv7s arm64
e.使用finder來導航到: /opt/libjpeg-turbo/lib,將 libjpeg.a檔案新增到你的工程。
f. XCode-> Targets -> General -> Linked frameworks and libraries -> Click on the add button placed at the bottom -> include the libjpeg.a file
g.編譯專案,問題解決(可能還有問題包這個錯誤:


螢幕快照 2016-10-27 下午2.49.10.png

解決方法:在Build Settings搜尋 enable_bitcode改為No