XCode: duplicate symbol

XCode: duplicate symbol  


http://hmifly.blog.163.com/blog/static/128583507201342752241781/



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


項目需要,同時引用ZBar和QQ授權登錄SDK,由於二者均使用了Base64處理數據,XCode編譯時報錯:

duplicate symbol _base64_encode in:

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

...\TencentOpenAPI(base64.o)


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


解決方案:

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

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


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

以下是從外國友人那獲取的終極解決策略,方案是修改類庫:
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的部分從任意一個類庫中刪除,你可以相應的修改這個過程。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章