() Static library (. a) packaging,.
A library is a collection of code. Based on the degree of code exposure, it can be divided into open source libraries and closed source libraries.
The closed source database mainly includes static and dynamic libraries, which are compiled binary files and cannot be implemented.
The extension name of the static library is. a or. framework, and the dynamic library is. dylib and. framework.
When a static library is linked, it will completely copy the database to an executable file, and redundant copies will be made when used for multiple times.
Dynamic libraries are not copied when they are linked, and the system dynamically loads them when the program is running. Multiple programs share a dynamic library, saving memory.
Note: If the project contains a self-made dynamic library, the upload to the App Store will be rejected. If you want to upload the App, you should select a static library.
The static library is used to extract core code for other projects.
Applications of static libraries: MRC libraries (such as cocos2d-iphone) for ARC projects can be packaged into static libraries.
The following is a simple example to illustrate the process of creating a static database.
In this example, a class method is implemented to obtain an image.
① Create a static library project:
② Create a new class and use the class method to read an image.
It should be noted that, in order to prevent conflicts between resources and resources of the database itself when others use the static library, the library resources are often put in a Bundle. The method is to create a folder and rename it the extended name. bundle, right-click to display the package content and place it to the resource. For example, create SGTool. bundle, which is used to obtain an image.
Class description and implementation:
//// SGTool.h// staticDemo//// Created by 11 on 8/2/15.// Copyright (c) 2015 soulghost. All rights reserved.//#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface SGTool : NSObject+ (UIImage*)getImage;@end
//// SGTool.m// staticDemo//// Created by 11 on 8/2/15.// Copyright (c) 2015 soulghost. All rights reserved.//#import "SGTool.h"@implementation SGTool+ (UIImage *)getImage{ NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"button.png" ofType:nil inDirectory:@"SGTool.bundle"]; return [UIImage imageWithContentsOfFile:imagePath]; }@end③ Click Build Phases in the project configuration and add the exposed header Files to Copy Files. These Files are used for calling methods and viewing static library content, but. m is not exposed.
④ Compile the project under the real machine and simulator, right-click the. a file under the Products folder and choose Show in Finder. a static library for the real machine and simulator is displayed.
You only need to import the. a and include files, and then drag the bundle into the project to use the static library. Generally, these three files are packaged and named as lib, for example:
⑤ Because the architecture of the real machine is different from that of the simulator, the real machine is generally arm64 and the simulator is x86, so the static library is different. In order to make a static library that can be used by both, use the lipo command to merge static databases.
Use the static library of the real machine when publishing.
Lipo-create <file1.a> <file2.a>-output <mixfile. a>
lipo -create Debug-iphoneos/libstaticDemo.a Debug-iphonesimulator/libstaticDemo.a -output SGTool.a
In addition, you can use lipo-info <file. a> to view the architecture supported by the static library.
⑥ Differentiate static libraries of Debug and Release:
The default value is Debug, which provides complete symbolic information for debugging without code optimization.
It can be changed to Release, and the code is optimized. It does not contain complete symbolic information, and the execution speed is slightly improved.
Shows the change method:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.