现象


最近项目组在做一些第三方功能的集成,不止一次的遇到第三方库冲突的问题,报错如下:

duplicate           symbol           _OBJC_METACLASS_          $          _JKSerializer                     in          :


              /          Users          /          tony          /          Desktop          /          XXXProject          /          Lib          /          libMiPushSDK          .          a          (          JSONKit          .          o          )


              /          Users          /          tony          /          Library          /          Developer          /          Xcode          /          DerivedData          /          XXXProject          -          boqkajmzatzxohbyrrhklfiuknic          /          Build          /          Products          /          Debug          -          iphoneos          /          libPods          .          a          (          JSONKit          .          o          )


ld          :                     24                     duplicate           symbols           for           architecture           armv7


clang          : error          : linker                     command           failed           with           exit           code                     1                     (          use                     -          v                     to           see           invocation          )






在这个报错中,原因是第三方中自己打包了JSONKit库,而我们的项目中也使用了这个库。这种情况需要我们将第三方SDK中冲突的库移除掉,即将它拆包后重打包处理。

解决步骤

首先按照上述错误中提到的路径找到库文件:/Users/tony/Desktop/XXXProject/Lib/libMiPushSDK.a,将它拷贝一份出来做进一步处理。

cd                    ~          /                    &&                    mkdir          librepack                    &&                    cd          librepack


cp                    /          Users          /          tony          /          Desktop          /          XXXProject          /          Lib          /          libMiPushSDK          .          a                    .          /          libx          .          a





lipo -info libx.a

如果提示fat file,那么代表这个包是支持多平台的,例如armv7,armv7s,i386等,这需要我们逐一做解包重打包操作。否则我们只需要做一次[1-6]操作即可mkdir armv7lipo libx.a -thin armv7 -output armv7/libx-armv7.aar -t armv7/libx-armv7.acd armv7 && ar xv libx-armv7.arm JSONKit.ocd .. && ar rcs libx-armv7.a armv7/*.o

  1. ,可以再次使用[2]中命令确认是否已成功将文件去除
  2. 将其他几个平台(armv7s, i386)包逐一做上述[1-6]操作
  3. 重新合并为fat file的.a文件:
    lipo -create libx-armv7.a libx-armv7s.a libx-i386.a -output libMiPushSDK-new.a
  4. 拷贝到项目中覆盖源文件:
    cp libMiPushSDK-new.a /Users/tony/Desktop/XXXProject/Lib/libMiPushSDK.a







iOS编程:第三方静态库(.a文件)处理

在引用第三方库时,不时的会碰到诸如库冲突、库包含了某些禁用的API等问题,而这些库往往都被打包成了静态库文件(即 .a文件)来使用。这时就需要我们能够去对Object file进行一些必要的处理调整。如检索信息,移除冲突的库等。以下是一些常用的方法:

一、 查找.a文件中是否包含相关信息,如苹果禁用的unique Identifier。

find  .  -name *.a |xargs grep uniqueIdentifier

查看object file文件信息:
nm  xxx.a | grep ".o:" | sed "s/.(//g" | sed "s/).//g" | uniq

二、去除不同的.a文件之间内含冲突的库,如openUDID库的冲突。

1. 查看architectures
 lipo  -info  libx.a 2. 如果是fat file,处理成non-fat
 lipo  libx.a  -thin armv7  -output  libx-armv7.a 3. 解压出object file
 ar  xv  libx-armv7.a 4. 打包object file
 ar  rcs   libx.a   *.o 5. 生成Fat file
 lipo  -create  Libarmv7.a  -output  libx.a合并.a文件
lipo  -create  Libarmv6.a  Libarmv7.a  -output  Lib.a 
 
 
 
 
 
 
 
 
 --------------------------------------------------------------------------------------------------------------------------------------
下面是我自己解包打包的过程
 -------------------------------------------------------------------------------------------------------------------------------------
 
 
Last login: Thu Sep 29 08:47:50 on ttys001
taijitongdeiMac:~ taiyoshimichi$ cd /Users/taiyoshimichi/Desktop/打猎机/打猎机备份/nvt/MobileNVTKit
-bash: cd: /Users/taiyoshimichi/Desktop/打猎机/打猎机备份/nvt/MobileNVTKit: Not a directory
taijitongdeiMac:~ taiyoshimichi$ cd /Users/taiyoshimichi/Desktop/打猎机/打猎机备份/nvt
taijitongdeiMac:nvt taiyoshimichi$ lipo -info MobileNVTKit
Architectures in the fat file: MobileNVTKit are: armv7 armv7s i386 x86_64 arm64 
taijitongdeiMac:nvt taiyoshimichi$ lipo MobileNVTKit -thin armv7 -output armv7/MV-armv7
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: can't create output file: armv7/MV-armv7 (No such file or directory)
taijitongdeiMac:nvt taiyoshimichi$ lipo MobileNVTKit -thin armv7 -output armv7/MV-armv7.a
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: can't create output file: armv7/MV-armv7.a (No such file or directory)
taijitongdeiMac:nvt taiyoshimichi$ mkdir armv7
taijitongdeiMac:nvt taiyoshimichi$ lipo MobileNVTKit -thin armv7 -output armv7/MV-armv7
taijitongdeiMac:nvt taiyoshimichi$ ar -t armv7/NV-armv7
ar: armv7/NV-armv7: No such file or directory
taijitongdeiMac:nvt taiyoshimichi$ ar -t armv7/MV-armv7
__.SYMDEF
NVTFileDownload.o
NVTKit.o
NVTControl.o
GDataXMLNode.o
taijitongdeiMac:nvt taiyoshimichi$ cd armv7
taijitongdeiMac:armv7 taiyoshimichi$ ar xv MV-armv7
x - __.SYMDEF
x - NVTFileDownload.o
x - NVTKit.o
x - NVTControl.o
x - GDataXMLNode.o
taijitongdeiMac:armv7 taiyoshimichi$ rm GDataXMLNodo.o
rm: GDataXMLNodo.o: No such file or directory
taijitongdeiMac:armv7 taiyoshimichi$ rm GDataXMLNode.o
taijitongdeiMac:armv7 taiyoshimichi$ cd..
-bash: cd..: command not found
taijitongdeiMac:armv7 taiyoshimichi$ cd ..
taijitongdeiMac:nvt taiyoshimichi$ ar rcs MV-armv7 armv7/*.o
taijitongdeiMac:nvt taiyoshimichi$ lipo MobileNVTKit -thin armv7s -output armv7s/MV-armv7s
taijitongdeiMac:nvt taiyoshimichi$ cd armv7s
taijitongdeiMac:armv7s taiyoshimichi$ ar xv MV-armv7s
x - __.SYMDEF
x - NVTFileDownload.o
x - NVTKit.o
x - NVTControl.o
x - GDataXMLNode.o
taijitongdeiMac:armv7s taiyoshimichi$ rm GDataXMLNode.o
taijitongdeiMac:armv7s taiyoshimichi$ cd ..
taijitongdeiMac:nvt taiyoshimichi$ ar rcs MV-armv7s armv7s/*.o
taijitongdeiMac:nvt taiyoshimichi$ lipo MobileNVTKit -thin i386 -output i386/MV-i386
taijitongdeiMac:nvt taiyoshimichi$ cd i386
taijitongdeiMac:i386 taiyoshimichi$ ar xv MV-i386
x - __.SYMDEF
x - NVTFileDownload.o
x - NVTKit.o
x - NVTControl.o
x - GDataXMLNode.o
taijitongdeiMac:i386 taiyoshimichi$ rm GDataXMLNode.o
taijitongdeiMac:i386 taiyoshimichi$ cd ..
taijitongdeiMac:nvt taiyoshimichi$ ar rcs MV-i386 i386/*.o
taijitongdeiMac:nvt taiyoshimichi$ lipo MobileNVTKit -thin x86_64 -output x86_64/MV-x86_64
taijitongdeiMac:nvt taiyoshimichi$ cd x86_64
taijitongdeiMac:x86_64 taiyoshimichi$ ar xv MV-x86_64
x - __.SYMDEF
x - NVTFileDownload.o
x - NVTKit.o
x - NVTControl.o
x - GDataXMLNode.o
taijitongdeiMac:x86_64 taiyoshimichi$ rm GDataXMLNode.o
taijitongdeiMac:x86_64 taiyoshimichi$ cd ..
taijitongdeiMac:nvt taiyoshimichi$ ar rcs MV-x86_64 x86_64/*.o
taijitongdeiMac:nvt taiyoshimichi$ lipo MobileNVTKit -thin arm64 -output arm64/MV-arm64
taijitongdeiMac:nvt taiyoshimichi$ cd arm64
taijitongdeiMac:arm64 taiyoshimichi$ ar xv MV-arm64
x - __.SYMDEF
x - NVTFileDownload.o
x - NVTKit.o
x - NVTControl.o
x - GDataXMLNode.o
taijitongdeiMac:arm64 taiyoshimichi$ rm GDataXMLNode.o
taijitongdeiMac:arm64 taiyoshimichi$ cd ..
taijitongdeiMac:nvt taiyoshimichi$ ar rcs MV-arm64 arm64/*.o
taijitongdeiMac:nvt taiyoshimichi$ lipo -create MV-armv7 MV-armv7s MV-arm64 MV-i386 MV-x86_64 -output MObileNVTKit
taijitongdeiMac:nvt taiyoshimichi$