iOS packaged static library (full article)

Source: Internet
Author: User

1. What is a library?

The so-called Library is a collection of program code, is a way to share program code.

2, the classification of the library

Depending on the open source of program code, the library can be divided into two categories

    • Open Source Library
      The source code is public and you can see the specific implementation. such as GitHub's more famous third-party framework afnetworking, Sdwebimage.
    • Closed Source Library
      Unfair open source code, only the interface of the public call, do not see the specific implementation, is a compiled binary file. This is common in some company SDK packages, such as the Gold Map SDK, the ring letter Instant Messenger SDK and so on. The closed source Library is divided into two categories: static library and dynamic library. This article focuses on the static library.

3, the existence of static libraries and dynamic libraries and the use of different forms

form of existence:

    • Static Library
      With ". A" or ". Framework" as the file suffix name
    • Dynamic Library
      Take ". Dylib" or ". Framework" as the file suffix name (after Xcode7. TBD instead of. Dylib)

Usage differences:

    • When a static library is linked, it is fully copied to the executable file, and multiple copies are used multiple times.

When a static library is used by a program
    • Dynamic library links are not duplicated, and the program is dynamically loaded into memory by the system for program invocation. And the system only loads once, multiple programs are shared, saving memory.

When a dynamic library is used by a program


4. CPU Architecture of IOS device

Simulator:
4s-5: i386
5s-iphone X (Pack live IPhone SE): x86_64

Real-Machine (iOS device):
Armv6:iphone, iphone 2, iphone 3G, ipod touch (first generation), ipod touch (second generation)
Armv7:iphone 3Gs, iphone 4, iphone 4s, ipad, ipad 2
Armv7s:iphone 5, IPhone 5c (Static library as long as support ARMV7, you can run on the armv7s architecture)
Arm64:iphone 5s, iphone 6, iphone 6 Plus, iphone 6s, iphone 6s Plus, ipad Air, ipad Air2, ipad mini2, ipad Mini3

Note: iphone 7, iphone 7 Plus, iphone 8, iphone 8 Plus, iphone x real-time What is the architecture is temporarily unknown (too poor, can't afford ~), but the simulator is x86_64.

Third, packaging Static library

Because static inventory is in two forms, we look first. A static library package

    • . A file Static library packaging

1. Open Xcode to create a new project, here take Xcode8 as an example, select the project as follows:


Create a new project

2, after the creation of the project, then create a tool class Staticlibtool, add a method for testing


Create a tool class, add test methods

The STATICLIBTOOL.M file is implemented as follows


TATICLIBTOOL.M File Implementation

3. Run the project for packaging


Run the Engineering package

After running, we can see that the LIBSTATICLIB.A file under the Products folder in the project is changed from red to black. Right-click the show in Finder to find it in its directory. This is what we packed. A static file.


Packed. A static file

But that's it? Of course not, we know that the greatest significance of the static library is the implementation of the hidden code, but this is also hidden too thoroughly, always expose some interface or header file for people to call it.

4. Open and oral documents

Targets->build phases->copy files-> "+" you need to expose the header file

Here we make the new test class StaticLibTool.h public


Public access to oral documents

After exposing the header file, we re-run the package as described in the 1, 2, 3 process, we get a header file and a. A static library (such as), and that's exactly what we need.


Re-run the package

5. Create a new project to run, drag the two packaged files into the project test


Test

Select the Iphone7 simulator to run, the program runs normally, click on the simulator screen, print the log as follows:


Log output

We can see that the output is not a problem, packaging. A static library is done.

But don't be too happy. When I switch the simulator to Iphone5 run, the compilation does not pass directly, the error is as follows:


Compile error on IPhone 5 Emulator runtime

What does "Undefined symbols for architecture i386" mean? This means that our LIBSTATICLIB.A static library does not support the i386 architecture. What the hell is that i386? Not clear can be pulled up to see "IOS Device CPU Architecture", there is not much to explain.

The IPhone 5 Simulator is exactly the i386 architecture, and our packaged static libraries are not supported. But the iphone 7 simulator runs without a problem, which means that our packaged static library just supports the CPU architecture of the iphone 7 simulator x86_64. How to view the schemas supported by the static library, see the next step.

6, terminal view the schema supported by the static library

Terminal->CD Enter library file path,lipo-info library name


Terminal view the schemas supported by the static library

As you can see, our static library only supports the x86_64 architecture, meaning that this static library can only run emulator devices between Iphone5s-iphone7plus. So just when we ran the Iphone5 simulator, the compilation would be an error.

Here you can further explain that when you package a static library, you run with what emulator you use, and the static library that you package supports the architecture of the emulator, and just as I packaged it with IPhone7, I only supported the schema x86_64. So this is too much trouble, can you package a static library to support a variety of architecture simulator? The answer is yes, take a look at the next step.

7. Set up all simulator architectures

Build Active Architecture, buildseting, project, only set to No


Settings to fit all emulator architectures

After the setup is complete, we rerun the package static library file (at this point you can choose a simulator), according to the above 6th terminal to view its supported architecture, we can see that the result of the terminal output is both I386 and x86_64, which means that all the emulators are supported at the same time.

To pack here. A static library has come to an end, but according to the above process packaging can only run on the simulator, the real machine is not running, because the iOS real device with the emulator architecture is not the same (how not to pull up to see the same), so it is not finished (I do not want to AH), see the next step

8, packaging support the static library of the real-machine architecture

All the process is the same as above, but we have to choose to run the package when the real machine to run, such as you can choose to plug in the real machine, you can also choose generic iOS Devices. Of course, don't forget. Setup supports all true machine model architectures: Build Active Architecture only set to No.


Packaging a static library that supports a real-machine architecture

We can look at the packaged terminal to see the results as follows:


Terminal output Results

You can see support for both ARMV7 and arm64, which is supported on all iOS devices. Okay, here we go. A static library is an over.

If you want to support both the emulator and the real machine, use command compositing. A static library: Lipo-create name1.a The path where name2.a is located-output newname.a

    • . frameworke File Static Library packaging

1, still Xcode create a new project Frameworkelib, select the project as follows:


Create a new project

After creation we can see that the project itself comes with a FrameworkeLib.h file, which is something like a master header file


FrameworkeLib.h file

2, create the class that need to test, in order to facilitate I put the above packaging. A test class Staticlibtool directly to use.

3. Settings support all emulator architectures or real-machine architectures (and packaging. A 7th Step)

4. Public header File

Target-build phases-headers-Drag the header file you want to expose from project to public


Exposing header Files

5. A static library is set up for packaging. Because the dynamic library can also be in the framework form, so need to set, otherwise the default is to hit the dynamic library (Note: If you want to go online appsotre, must be changed to static library, otherwise audit pass)

target->buildsetting Search keyword Mach->mach-o Type set to static Library (this default option is dynamic)


The settings are packaged in a static library

6, check the real machine or simulator to run the device packaging (as with packaging. A), the Frameworkelib.framework file under the Products folder is changed from red to Black, right-click Show in Finder as follows:


Packaging results

Frameworkelib.framework is dragged into the project and can be used directly, where it is no longer tested. It is also important to add that when you package a static library, you need to be aware of whether the package is beta (Debug) or release, which is based on your own needs, and how to set it up in the next step.

7. Set up beta and release versions of packaged static libraries (. A and. Frameworke)

Product, scheme, Edit scheme, run-> select Debug or Release


If you want to support both the emulator and the real machine, and. A is similar, use the command Composition Framework Library:lipo-create <name1>.framework/<name1> <name2>. framework/<name2>-output newname

iOS packaged static library (full article)

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.