Static libraries and dynamic libraries in IOS, differentiating, authoring, and using

Source: Internet
Author: User

If we have some features to use for others, but do not want to expose the code implementation, such as the gold map, third-party login sharing and so on, we will be packaged into a library. There are two kinds of library: static library and dynamic library.

Static libraries: With .a and .framework for file suffix names.
Dynamic libraries: With .tbd (previously called .dylib ) and .framework as file suffix names.

The difference between a static library and a dynamic library

Static libraries: Links are fully copied to the executable file, and multiple copies are used multiple times.
Dynamic Library: The link is not copied, the program is dynamically loaded into memory by the system, the system is loaded only once, multiple programs common (such as system UIKit.framework , etc.), save memory.

But Apple does not allow its own dynamic library, otherwise the audit will not pass.

Let's take a look at the architecture of the iOS device, because here's what you need:

Simulator:
Iphone4s-ipnone5:i386
Iphone5s-iphone7 plus:x86_64

Real machine:
Iphone3gs-iphone4s:armv7
iphone5-iphone5c:armv7s
Iphone5s-iphone6s:plus:arm64

A static library that supports ARMV7 can run correctly on armv7s.

. A static library of the production

1, first create a new Xcode project test, you need to select the following template:

Select a template

This looks like when the creation is complete:

Create a project

2, we delete the default generated Test.h and TEST.M, re-create a class printstring, in this class to add a simple way to print a string:

PrintString.h

Printstring.m

3. Select Add Public header file

In order to let the user know which methods are available, we need to expose the header file, here we open PrintString.h:

Select Add Public header file

4. Modify the configuration

We need to modify build Active Architecture only to No, otherwise the generated static library will only support the schema of the currently selected device.

Modify Configuration

5. Then compile

We have selected generic IOS device and any simulator compiled once, after compiling, we will see the project in the Products folder libtest.a from red to black, and then show in Finder, look at the generated file:

Untitled.gif

We see that it generates. A static library for both the real machine and the simulator. It contains the header files we have chosen to expose.

Let's take a look at the framework supported by the Static Library: command for lipo-info Static library name

View supported frameworks for static libraries

We see that the Debug-iphoneos inside the static library supported by ARMV7 and arm64 so it can only be used in the real machine, the simulator will be error. Debug-iphonesimulator inside the static library supported by I386 and x86_64, so it can only be used in the simulator, the real machine will be error.

If you want to have the emulator and the real machine generic a static library, we can use the Terminal command to implement. Command format: Lipo-create the absolute path of the first. A file the absolute path of the second. A file-output the final. A file path:

Generating a common static library

We saw that a new Libtest.a file was generated. This static library supports all emulators and all real machines. Then we create a folder and put the. A and header files in, and we end up needing to use this folder:

New Folder

Note: For ease of development, we can use the generated generic static library, but the final on-line use we can only import the real machine, so the volume of the project will be smaller.

Use the generated. A static library

Create a new project, drag the general static library above, import the header file, you can use the method inside. After experimentation, the static libraries we generate can print the string successfully on both the real machine and the emulator:

Paste_image.png

. Frameworke Static Library production

1, first create a new Xcode project libtest, you need to select the following template:

Select a template

This looks like when the creation is complete:

Create complete

After creation we can see that the project itself comes with a LibTest.h file and a info.plist file.

2, we create a class printstring, add a simple way to print a string:

PrintString.h

Printstring

3. Select Add Public header file

In order to let the user know which methods are available, we need to expose the header file, and we need to drag and drop the header file that is exposed in project Target->build phases->headers into the pulic. Here we open PrintString.h:

Select Add Public header file

Note: Other classes of import in the exposed header file also have to be added to public. If you do not want to expose the import class, use @class in the header file and then import it in the corresponding. m file.

4. Set support for all architectures (as with. a production)

5, modify the generated mach-o format, because the dynamic library can also be in the form of the framework, so you need to set, otherwise the default is to hit the dynamic library. Set the Target->buildsetting->mach-o Type to the static library (the default is the dynamic library):

Modify the generated Mach-o format

6. Compiling

We separately choose generic IOS device and any one of the emulators compiled once, after compiling, we will see the project in the Products folder libtest.framework from red to black, and then show in Finder, Look at the resulting file:

Compile

We see that it generates libtest.framework static libraries for both the real machine and the simulator.

Let's look at the framework supported by the static library: Unlike above, the command is a binary file name under the Lipo-info framework

View supported frameworks for static libraries

If you want to have the emulator and the real machine generic a static library, we can use the Terminal command to implement. The merge command differs from the above: the framework static Library merges not the framework, but the binaries under the framework, and the commands are:

Lipo-create Absolute path of the first framework under binary file the absolute path of the binary file under the second framework-output the final binary file path:

Merging binary files

Then replace the binaries in any one framework with the merged binaries, and then add the framework to the project you want to use.

Use the generated. Framework Static Library

Create a new project, drag the static library into the header file, you can use the method inside. After experimentation, the static libraries we generate can print the string successfully on both the real machine and the emulator:

Paste_image.png

Attention:

If there is a category class in the static library, the other Linker flags need to add the parameter-OBJC or-all_load in the project configuration that uses the static library.

If you are creating a framework class that uses. TBD, you need to import the. TBD Dynamic Library in the actual project.

Run the Debug static library

If you are the person who developed the static library, you will find that the above method is just to make a static library, and there is no way to run the look and debug bugs, this time we can:

1. Create a new normal engineering test to develop a static library:

New Project

The construction of a good project

2. Add a static library target

Add a target for a static library

We saw that it generated a few things:

A framework target: Modifies the configuration of static libraries in this case, such as the supported schemas, the header files to be exposed, and the configuration of the Mach-o.
A Libtest folder: The classes inside the static library are placed here.
Libtest.framework below the product folder: Here the show in Finder finds the static libraries generated after compilation.

3. Developing Debugging Code

Developing debug Code

We see that the program works and can run camp in the dynamic library. Convenient for us to debug.

4, to ensure that the code is not a problem, select the corresponding target compilation generated.

Compile Build

5, the following process is the same as above.

Static libraries and dynamic libraries in IOS, differentiating, authoring, and using

Related 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.