轉--xcode duplicate symbol問題

遇到引用庫重複定義的問題,須要解決。html

 
項目須要,同時引用ZBar和QQ受權登陸SDK,因爲兩者均使用了Base64處理數據,XCode編譯時報錯:

duplicate symbol _base64_encode in:ios

 

...\libzbar.a(symbol.o)架構

...\TencentOpenAPI(base64.o)app

 

意思就是在這兩個庫中都定義了_base64_encode,編譯器認爲你重複定義了。至於爲何編譯器認爲重複定義,我的認爲編譯器編譯全局變量時會根據名字進行編譯,會把相同名稱的全局變量編譯爲相同變量,也就是多個編譯成一個,而編譯器認爲這樣可能會引發錯誤,就提醒用戶這裏有錯。ide

 

解決方案:ui

參考了:http://blog.sina.com.cn/s/blog_4beb28f301012bl6.htmlthis

刪掉了 set building->other linker flag-> -all_loadspa

 

ios的Framework是共享動態庫,不會被打包到app中,非系統Framework靜態庫都會被打包到app中,因此會產生"Duplicate Symbol"的錯誤。
在Build Settings->Other link flags中刪除全部的-all_load與-force_load, XCode會很smart的去掉"Duplicate Symbol"。code

 
如下是從外國友人那獲取的終極解決策略,方案是修改類庫:
I'm going to assume that these are two third party libraries that have only provided you with the .a files and not the source code. You can use libtool, lipo and ar on the terminal to extract and recombine the files.
假設有兩個三方類庫僅提供給你了.a文件,沒有源碼,你能夠經過libtool, lipo和ar在terminal中解壓合併他們。
To see what architectures are in the file:
查看文件都支持了什麼架構
$ lipo -info libTapjoy.a
Architectures in the fat file: libTapjoy.a are: armv6 i386

Then to extract just armv6, for example:
而後只解壓armv6,例如
$ lipo -extract_family armv6 -output libTapjoy-armv6.a libTapjoy.a
$ mkdir armv6
$ cd armv6
$ ar -x ../libTapjoy-armv6.a

You can then extract the same architecture from the other library into the same directory and then recombine them like so:
你能夠從另外一個類庫中解壓一樣架構的部分,而後將二者合併在一塊兒
$ libtool -static -o ../lib-armv6.a *.o

And then finally, after you've done this with each architecture, you can combine them again with lipo:
如上所示,你能夠將全部架構都按照這個流程走一遍,而後合併
$ cd ..
$ lipo -create -output lib.a lib-armv6.a lib-i386.a

This should get rid of any duplicate symbols, but will also combine the two libraries into one. If you want to keep them separate, or just delete the duplicate from one library, you can modify the process accordingly.
這個過程不只解決掉了duplicate symbols的問題,也將兩個類庫合併爲一個。若是你想分別保存兩個類庫,你能夠將duplicate的部分從任意一個類庫中刪除,你能夠相應的修改這個過程。
相關文章
相關標籤/搜索