IOS: Static Library

Source: Internet
Author: User

I. Introduction to the Static library1. What is a library?

A library is a collection of program code, a way to share program code

2. Classification of libraries?
    • Open Source Library

      Open source code, can see concrete implementation

      such as Mjextension, Mjrefresh, afnetworking ...

    • Closed Source Library

      Unfair open source code, is a compiled binary file, do not see the specific implementation

      Mainly divided into: static Library and dynamic library

3. What is the existing form of a static library?
    • . A
    • . framework
4. What are the existing forms of dynamic libraries?
    • . dylib
    • . framework
5. What is the difference between a static library and a dynamic library?
    • When a static library is linked, it is fully copied to the executable file; be used multiple times, there are multiple copies;

    • Dynamic libraries are not duplicated, only one copy. Dynamically loaded into memory while the program is running; The system only loads once, multiple programs are shared, saving memory;

    • But!!!! If you use your own dynamic library in the project, no shelves are allowed!

    • Again, but!!! WWDC2014 published on the iOS8 open dynamic loading Dylib interface that means that open the dynamic library mount
6. Static Library application scenario?
    • Protect your core code

      Domestic enterprises, mastering the core technology, but also want more programmers to use its technology, so the use of "closed source" way to develop

        例如:百度地图,友盟,JPush等
    • The MRC project, packaged into a static library, can be used directly under ARC, without the need to convert
7. What are the characteristics of a static library?

. A +. h

Do not see the code for the specific implementation

two. Production of static libraries (. A)1. Large steps to generate a static library
1. 创建项目时, 直接选择静态库(.a)2. 设置需要暴漏的头文件    TARGETS -> Build Phases -> Copy Files    把需要暴漏的头文件添加进来即可3. 在模拟器环境下编译(得到模拟器环境下的静态库)    选择模拟器6S,编译4. 在真机环境下编译(得到真机环境下的静态库)
2. Static Library Use Testing
1.使用模拟器的静态库, 拖入测试工程    使用6S模拟器进行测试, 通过    使用真机, 编译; 失败    使用低型号模拟器测试,; 失败2.测试结果分析    模拟器下的静态库和真机下的静态库不能共用    主要原因是模拟器和真机CPU架构不一样(各个模拟器型号之间架构也不一样)3.注意静态库所支持的架构    不同机型的CPU, 对应的架构不同;    模拟器:        4s----5  : i386        5s----6sPlus : x86_64    真机:        3gs---4s : armv7        5/5c : armv7s(armv7兼容armv7s)        5s---6sPlus: arm644.查看静态库支持的架构    lipo -info 库文件    分别选中不同的模拟器, 进行编译, 查看不同的静态库支持架构5.怎样一次编译支持多个架构的的静态库?    问题描述:        正常情况下, 需要选中每一个模拟器进行编译, 生成支持对应架构的静态库. 然后合并; 非常蛋疼    解决方案:        Build Settings -> Build Active -> NO        表示不止编译活跃的架构, 让所有的架构都编译
3. Version of the static library file
1.调试版本    1.真机-Debug版本    2.模拟器-Debug版本    特点        - 调试版本会包含完整的符号信息,以方便调试        - 调试版本不会对代码进行优化2.发布版本    1.真机-Release版本    2.模拟器-Release版本    特点        - 发布版本不会包含完整的符号信息        - 发布版本的执行代码是进行过优化的        - 发布版本的大小会比调试版本的略小        - 在执行速度方面,发布版本会更快些,但不意味着会有显著的提升3.怎样生成不同版本?    项目 -> Edit Scheme -> Run -> Release/Debug 分别进行编译4.如果想要一个静态库, 既可以在模拟器上运行, 也可以在真机上运行怎么做?    因为静态库针对于模拟器和真机生成了不同版本(支持不同架构), 所以没法同时运行    解决方案:        静态库的合并            检测.a的类型                $ lipo -info libCZTools.a            合并.a                lipo -create Debug-iphoneos/libTools.a Debug-iphonesimulator/libTools.a -output libTools.a            特点                # 合并.a的好处,开发过程中既可以在真机上调试,也可以在模拟器上调试                # 合并.a的坏处,如果静态库太大,合并打包后,会非常大,因此很多第三方的静态库的.a是区分版本的                # 今后在使用.a时一定注意版本
three. Static library production (. Framework)
    1. New feature, direct selection. Framework Static Library
    2. Compile, set compile all schemas

    3. The default is to create a dynamic library, you need to set the link type

      Target---Build settings-> search Mach-o Type; Change to Static library

Four. Summary1. Complete correct steps for static library packaging!!
1. 确定是静态库    .a 的肯定是静态库    .framework的需要设置链接类型        target -> Build Settings-> 搜索 Mach-o Type ; 改为静态库

2. 确定支持模拟器或者真机中的所有架构    Build Settings -> Build Active -> NO        表示不止编译活跃的架构, 让所有的架构都编译3. 提供的静态库应该是release版本    项目 -> Edit Scheme -> Run -> Release/Debug 分别进行编译
2.: What is the difference between a static library and a. Framework static library?
1> .a是一个纯二进制文件,  .framework中除了有二进制文件之外还有资源文件2> .a文件不能直接使用, 至少要有.h文件的配合; .framework文件可以直接使用3> .a + .h + sourceFile = .framework4>  建议使用.framework
3. Common problems in the development of static libraries
问题1: 有些第三方库会使用到一些图片素材,例如公司的logo等。    由于Xcode默认在编译时会把所有的素材文件导入到mainBundle中,可能与使用静态库的程序冲突。    解决方案:        在静态库中如果要使用图片素材,会利用bundle的手段            1>  建立bundle,并且向其中添加图片            2>  创建一个类方法,返回图片            3>  编译            4>  调用方如果需要使用,需要导入 .h + .a + XXX.bundle问题2:如果用户需要导入的头文件过多怎么加?    建议使用一个主头文件包含其他头文件, 让用户只导入一个主头文件问题3: 静态库程序怎样测试?    静态库本身就是一个小项目, 实现某些功能, 但是这些功能在开发中也需要测试. 而测试代码又不能作为静态库的一部分    解决方案:        创建复合项目
4. Package The MRC project into a static library that can be used directly under ARC, without the need to convert

5. Add: Swift Dynamic Library package and use

Note: The static library is not supported by Swift

Want to leak out of the interface, learn to use the Public keyword

6. Consolidation of the Static library (. Framework)

Http://www.cnblogs.com/KeenLeung/p/5491332.html

IOS: Static Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.