Linux 从 iPhone 静态库中提取对象 (*.o) 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4578771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract object (*.o) files from an iPhone static library
提问by Brett
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.
我有一组iPhone 静态库(一个 *.a 文件),我只从中调用了几个类。我过去曾使用 AR(使用 linux 库)从静态库中提取目标文件,删除不需要的目标文件并重新存档。
However, when I try this with an iPhone compliled static library, I get the following error:
但是,当我使用 iPhone 编译的静态库尝试此操作时,出现以下错误:
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.
有谁知道如何从 iphone 编译的静态库中提取目标文件?这样做可能会减少最终文件的大小。
采纳答案by Brett
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,
那是因为您的 CustomiPhoneLib.a 是一个胖库,即包含多个目标架构的库,即 iOS 上的 armv6 和 armv7。您可以使用lipo
将特定架构提取到另一个 .a 文件中,随意使用ar
和ranlib
操作它,然后lipo
再次使用将操作后的 .a 文件重新组合成单个 .a 文件。例如,
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.
但是,由于您提到的原因,您不必这样做。如果链接器需要解析某些符号引用,它只会从库 (.a) 中提取对象 (.o) 文件。因此,如果库包含一个目标文件,其符号在链接过程中从未被引用(即未有效使用的符号),则该目标文件将不会使其成为可执行文件。
回答by Dipak Narigara
Code:
ar -t mylib.a
This will list all of the files in the archive.
Code:
ar -t mylib.a 这将列出存档中的所有文件。
Code:
ar -xv mylib.a myobj.o
This will extract the object give myobj.o from the library mylib.a.
Code:
ar -xv mylib.a myobj.o 这将从库 mylib.a 中提取对象给 myobj.o。