Introduction to Static Libraries

    1. What is a library?
    • A library is a collection of program code, a way of sharing program code
    1. Classification of libraries?
    • Open source library
      • Open source code, you can see the implementation
      • Examples include MJExtension, MJRefresh, AFNetworking…
    • Closed source library
      • Not open source code, is compiled after the binary file, do not see the specific implementation
      • It is mainly divided into static library and dynamic library
    1. How does a static library exist?
    • .a
    • .framework
    1. How does a dynamic library exist?
    • .dylib
    • .tbd
    • .framework
    1. What is the difference between static and dynamic libraries?
    • Static libraries are copied in their entirety to executable files when linked; Used many times, there are many copies;
    • Dynamic libraries do not copy, only one copy. Dynamically load the program into memory while it is running; The system only loads once, multiple programs share, saving memory;
    • But!!!!!!!!!! If you use your own dynamic library in the project, it is not allowed to be on the shelf!
    • But again!!!!!! WWDC2014 announced that apple has opened dylib interface for ios8, which means open dynamic library mount
    1. Static library application scenarios?
    • Protect your core code
    • Domestic enterprises, master the core technology, but also want more programmers to use its technology, so use the “closed source” way of development and use
    • For example: Baidu Map, Umeng, JPush and so on
    • MRC projects are packaged into static libraries that can be used directly under ARC without conversion
    • Improve project compilation speed
    1. Static library characteristics?
    • .a + .h
    • You can’t see the implementation code

Static library making (.a)

  • Generate static library big steps
      1. When creating a project, directly select the static library (.a)
      1. Set the header file that needs to be exposed
      • TARGETS ->Build Phases ->Copy Files
      1. Compile in simulator environment (get static library in simulator environment)- select simulator 6S and compile
      1. Compile in real environment (get static library in real environment)
  • Static library usage tests
    • 1. Use the static library of the simulator and drag it into the test project
      • Use 6S simulator to test, pass
      • Use real machine, compile; failure
      • Test using a low model simulator; failure
    • 2. Analysis of test results
      • Static libraries in emulators and real machines cannot be shared (architecture is different between emulators)
      • The main reason is that the CPU architecture of the emulator is different from that of the real machine
      1. Note the architecture supported by static libraries
      • Cpus of different models have different architectures.
      • The simulator:
        • 4s—-5 : i386
        • 5s—-6sPlus : x86_64
      • True machine:
        • 3gs—4s : armv7
        • 5/5C: ARMV7S (armV7 compatible with ARMV7s)
        • 5s—6sPlus: arm64
      • Check out the architectures supported by static libraries
        • Lipo-info library file
        • Select different emulators and compile to see the different static library support architectures
      1. How to compile a static library that supports more than one schema at a time?
      • Problem Description:
      • Normally, each emulator needs to be selected and compiled to generate a static library that supports the corresponding architecture. And then merge; Very egg pain
      • Solution:
      • Build Settings -> Build Active -> NO
      • Indicates compiling not only active schemas, but all schemas
  • The version of the static library file
    • Debug version
      • 1. The real -Debug version
      • 2. Simulator -Debug version
      • The characteristics of
        • The debug version contains complete symbolic information to facilitate debugging
        • The debug version does not optimize the code
    • The release
      • 1. The real machine-release version
      • 2. Emulator -Release version
      • The characteristics of
        • The release version will not contain complete symbolic information
        • The code executing the release is optimized
        • The size of the release version will be slightly smaller than the debug version
        • In terms of execution speed, the release will be faster, but that doesn’t mean there will be a significant improvement
    • How do YOU build different versions?
      • Project -> Edit Scheme -> Run -> Release/Debug compile separately
    • If you want a static library that can run on either an emulator or a real machine what do you do?
    • Because the static library is generated in different versions (supporting different architectures) for the simulator and the real machine, it cannot be run simultaneously
    • Solution: Static library consolidation
      • Check the type of. A. $lip-info libcztools. a
      • Merge. Lipo – create a Debug – iphoneos/libTools. A Debug – iphonesimulator/libTools. A – output libTools. A
      • The characteristics of
      • During the development process, it can be debugged on the real machine or on the simulator
      • Disadvantages of merge. A, if the static library is too large, merge and package, will be very large, so many third party static library. A is version-specific
      • Be sure to pay attention to the version when using.a in the future
    • Addendum: Disassemble libraries for a given schema
      • Lipo-thin architecture name Architecture path-output Target path

Static library Creation (.framework)

    1. Create a new project, directly select. Framework static library
    1. At compile time, set all schemas to compile

    1. The default is to create a dynamic library, need to set the link type
    • Target -> Build Settings-> search for Mach-o Type; Change to static library

conclusion

  • 1. Complete and correct steps for static library packaging!!
      1. Make sure the library is static
      • .a must be a static library
      • . Framework needs to set the link type
      • Target -> Build Settings-> search for Mach-o Type; Change to static library

      1. Make sure all architectures in the simulator or real machine are supported
      • Build Settings -> Build Active -> NO
      • Indicates compiling not only active schemas, but all schemas
      1. The supplied static library should be a release version
      • Project -> Edit Scheme -> Run -> Release/Debug compile separately
    1. What is the difference between.a static library and.framework static library?
    • A is a pure binary file. The. Framework has resource files in addition to binary files
    • A file can not be used directly, at least with. H file; .framework files can be used directly
    • .a + .h + sourceFile = .framework
    • Framework is recommended
    1. Common problems in static library development
    • Problem 1: Some third-party libraries use some image materials, such as the company logo.
    • Since Xcode imports all material files into mainBundle at compile time by default, it may conflict with programs that use static libraries.
    • Solution:
    • If you want to use images in a static library, you use the bundle
      • 1. Create the bundle and add the image source.bundle to it
      • 2. Create a class method that returns the image
      • 3. The compilation
      • 4. To use the bundle, the caller needs to import.h +.a + xxx. bundle
    • Question 2: How to add too many header files that the user needs to import?
      • It is recommended to use a primary header file to contain other header files, allowing users to import only one primary header file
    • Question 3: How are static library programs tested?
    • The static library itself is a small project that implements some functionality, but that functionality also needs to be tested during development. The test code cannot be part of a static library
    • Solution:
      • Creating composite projects

Static library operation

  • Compose libraries of different architectures
    • lipo -create xx xx -output xxx
  • Decomposition synthesis library
    • Lipo-thin architecture name xx-output XX
  • Removes a schema from the composite library
    • Lipo-remove architecture name xx-output XX
  • Check whether it is a dynamic library
    • file xx

Binary practice

  • Manual packaging scheme
    • Add library target. A /.framework depending on the situation
    • Import file reference
    • Generate the library
    • Move library to POD library
    • Modify podspec
s.source_files = 'XMGSegmentBar/Products/include/**'
s.public_header_files = 'XMGSegmentBar/Products/include/*.h'
s.ios.vendored_libraries = 'XMGSegmentBar/Products/lib/libXMGSegmentBarLib.a'
    spec.ios.vendored_frameworks =
Copy the code
  • Automatic packaging scheme
    • cocoapods-packager
      • Function quickly completes the packaging of class libraries
      • Sudo gem install Cocoapods-packager
      • use
        • Pod Package Spec file name
        • Pod Package spec file name –library –exclude-deps –spec-sources= Private index library path of the spec file,github.com/CocoaPods/S…
      • Note: The code must have been uploaded to the remote repository

Source code – binary switch

  • Sometimes in a host project, you need to see the source code and want to switch back and forth between source/binary files
  • Principles Control the direction of podSpec processes by setting the value of environment variables
  • The sample code
if ENV['IS_BINARY']
    s.source_files = 'XMGSegmentBar/Classes/*.h'
    s.public_header_files = 'XMGSegmentBar/Classes/*.h'
    s.ios.vendored_libraries = 'XMGSegmentBar/Products/lib/libXMGSegmentBarLib.a'

  else
    s.source_files = 'XMGSegmentBar/Classes/**/*'
  end
Copy the code

use

  • Clear the cache
    • pod cache clean –all
  • Delete the original project POD file
    • Before installation, be careful to add the environment variables you set
      • IS_BINARY=1 pod install