objective-clinuxiphone

Extract object (*.o) files from an iPhone static library


I have a set of iPhone static libraries (a *.a file) in which I only call a few of the classes from. I have used AR in the past (with linux libraries) to extract the object files from the static library, remove the unwanted object files and rearchive.

However, when I try this with an iPhone compliled static library, I get the following error:

ar: CustomiPhoneLib.a is a fat file (use libtool(1) or lipo(1) and ar(1) on it)
ar: CustomiPhoneLib.a: Inappropriate file type or format

Does anyone know how to extract the object files from an iphone compiled static library? Doing thie could potentially reduce the final file size.


Solution

  • That’s because your CustomiPhoneLib.a is a fat library, i.e., a library that contains more than one target architecture, namely armv6 and armv7 on iOS. You can use lipo to extract a specific architecture into another .a file, use ar and ranlib to manipulate it at will, and then use lipo again to recombine the manipulated .a files into a single .a fat file. For instance,

    lipo CustomiPhoneLib.a -thin armv6 -output CustomiPhoneLibarmv6.a
    lipo CustomiPhoneLib.a -thin armv7 -output CustomiPhoneLibarmv7.a
    ### use ar and ranlib at will on both files
    mv CustomiPhoneLib.a CustomiPhoneLib.a.original
    lipo CustomiPhoneLibarmv6.a CustomiPhoneLibarmv7.a -create -output CustomiPhoneLib.a
    

    However, you don’t have to do this for the reason you’ve mentioned. The linker will only pull object (.o) files from a library (.a) if it needs to resolve some symbol reference. Therefore, if a library contains an object file whose symbols are never referenced during the linking process (i.e., symbols that are not effectively used), that object file won’t make it into the executable.