bingoogolapple / bingoogolapple.github.io

个人主页。同时也通过 Issues 记录学习笔记
http://www.bingoogolapple.cn
86 stars 19 forks source link

iOS静态库 #98

Open bingoogolapple opened 8 years ago

bingoogolapple commented 8 years ago

静态库的基本制作

选择头文件一起打包

这种方式支持a/framework
Targets->Build Phases->Copy Files        如果是framework,在Subpath后面填上Headers。如果是a,在Subpath后面填上include/$(PRODUCT_NAME)

或

这种方式支持framework
Targets->Build Phases->Headers  添加或移动.h文件到Public中

每一个静态库支持的架构是固定的,模拟器和真机需分别打包 lipo -info liblibstatic.a(如果是framework则改成其目录中没有后缀名的子文件)

input file liblibstatic.a is not a fat file
Non-fat file: liblibstatic.a is architecture: i386

模拟器CPU架构

4s --> 5 : i386
5s --> 6plus : x86_64

真机CPU架构

3gs --> 4s : armv7
5/5c : armv7s  静态库只要支持了armv7,就可以跑在armv7s的架构上
5s --> 6plus : arm64

设置生成支持多种架构的库,但这种方法只能生成支持各个真机或各个模拟器版本的库

Targets->Build Settings->Build Active Architecture Only  选择No

Architectures in the fat file: liblibstatic.a are: i386 x86_64 

静态库合并(如果是framework则改成其目录中没有后缀名的子文件)

lipo -create 静态库1 静态库2 -output 新的静态库

lipo -info libstatic.a 
Architectures in the fat file: libstatic.a are: armv7 i386 x86_64 arm64 
bingoogolapple commented 8 years ago

调试a/framework静态库

  1. 点击TARGETS左下角的+来创建新的静态库Target
  2. 在TARGETS中选中可运行的Target->General->Linked Frameworks and Libraries 添加第一步创建的静态库Target
  3. 如果第一步创建的静态库Target中有用到xxx.bundle文件,在TARGETS中选中可运行的Target->Build Phases->Copy Bundle Resources 添加第一步创建的静态库Target中的xxx.bundle文件

默认情况下framework是动态库,须在General->Enbedded Binaries中添加动态库才能运行,否则会报如下错误

dyld: Library not loaded: @rpath/MathTool.framework/MathTool
  Referenced from: /Users/用户名/Library/Developer/CoreSimulator/Devices/508D83EC-8069-473A-83B3-41684EE669EF/data/Containers/Bundle/Application/BC87FC77-97A4-45F1-985A-E523C5105276/testframeworklib.app/testframeworklib
  Reason: image not found

将framework动态库变为framework静态库

选中TARGET->Build settings->搜索mach->Linking里的Mach-O Type改为Static Library

开启调试功能,在不同的工程中调试「如果是在同一个工程中同时存在一个可运行的target和一个静态库的target则不需要配置该选项」

Build settings->Generate Debug Symbols->Yes

「如果有Bundle文件,不要打进Framework中,删掉xcode自动添加到Build Phases->Copy Bundle Resources中的Bundle,单独拷贝」