【mac】lipo命令详解

Date: 2018.10.10


1、参考

https://www.jianshu.com/p/e590f041c5f6
https://blog.csdn.net/Qyee16/article/details/43742243
https://www.cnblogs.com/dabaopku/p/5698186.html
https://www.jianshu.com/p/fd0b9715f873
https://www.aliyun.com/jiaocheng/379498.html

2、前言

    在Xcode中,可以定义TargetArchitectures为StandardArchitectures,也就是编译器预先定义好的变量$(ARCH_STANDARD),这个自然包含了iOS app所需要支持的不同架构的binary库或App。但是如果我们的程序并非用Xcode编译而是用GNU make呢? 如何把多种不同架构的biarary组合在一起呢? 在进行mac、ios开发过程中,遇到需要制作在多个不同架构平台运行的程序,这就用到了mac下的lipo工具。
    lipo源于mac系统要制作兼容powerpc平台和intel平台的程序。
    lipo 是一个在 Mac OS X 中处理通用程序(Universal Binaries)的工具。现在发售或者提供下载的许多(几乎所有)程序都打上了“Universal”标志,意味着它们同时具有 PowerPC 和 Intel 芯片能够处理的代码。
    对于苹果系统来说, 丰富的设备包含了多种架构平台, armv7, arm64, i386, x86_64是最常见的指令集, 一个程序库为了兼容各个平台, 常常要把不同平台编译的程序合并起来, 生成所谓的胖文件(Fat File), 这样子开发者就不需要专门准备一套真机版本库和模拟器版本库了(在早期, 很多第三方库确实是提供了不同版本的二进制文件来减少库文件大小).
    因此对于一个可执行文件, 其实包含了3层结构: Universal Fat File -> Single Architecture Binary File -> Mach-O Object。

3、lipo命令详解

The lipo command creates or operates on universal (multi-architec-ture) files.
It only ever produces one output file, and never alters the input file.
The operations that lipo performs are:

  • listing the architecture types in a universal file;
  • creating a single universal file from one or more input files;
  • thinning out a single universal file to one specified architecture type;
  • extracting, replacing, and/or removing architectures types from the input file to create a single new universal output file.

Options:

-info 
Briefly list the architecture types in the input universal file (just the names of each architecture).
-detailed_info 
Display a detailed list of the architecture types in the input universal file (all the the information in the universal header, for each architecture in the file).
-create
Take the input files (or file) and create one universal output file from them.
-output output_file
Specifies its argument to be the output file.
  1. 查看静态库(通用文件)支持的CPU架构
lipo -info libname.a
lipo -detailed_info libname.a

例如:

lipo -info libsvac2dec.a 
Architectures in the fat file: libsvac2dec.a are: i386 x86_64 armv7 arm64
  1. 合并静态库
# lipo -create 静态库存放路径1  静态库存放路径2 ...  -output 整合后存放的路径
lipo  -create  libname-armv7.a   libname-armv7s.a   libname-i386.a  -output  libname.a
  1. 静态库拆分或者“瘦身”(提取单个平台)
# lipo 静态库源文件路径 -thin CPU架构名称 -output 拆分后文件存放路径
# 架构名为armv7/armv7s/arm64等,与lipo -info 输出的架构名一致
lipo  libname.a  -thin  armv7  -output  libname-armv7.a
  1. 提取、替换和去除指定CPU架构
lipo -extract armv7 libname.a -output libname_armv7.a    # 提取出armv7架构并新建一个通用文件,类似于-thin选项。
lipo -remove armv7 libname.a -output libname_exceptArmv7.a   #去除armv7架构
lipo libname.a -replace armv7 libreplace.a -output liboutput.a   #对输入文件libname.a中的armv7架构文件采用librepace.a进行替换,并输出到liboutput.a中

4、lipo工具所在路径

/Applications/Xcode6.4.app/Contents/Developer/ToolChains/XcodeDefault.xctoolchain/usr/bin

其中6.4表示当前使用的Xcode版本,需要根据实际使用的xcode版本进行更改。


THE END!

原文地址:https://www.cnblogs.com/SoaringLee/p/10532346.html