(102) Packaging of Static library (. a)

Source: Internet
Author: User

A library is a collection of code that is divided into open source and closed sources, depending on how exposed the code is.

The closed source library mainly includes static library and dynamic library, and it is compiled binary file, and can not see the concrete implementation.

The extension name of the static library is. A or. Framework, and dynamic libraries are the. Dylib and. Framework.

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

Dynamic library is not copied at link time, the system will load dynamically when the program runs, and multiple programs share a dynamic library, saving memory.

Note: If you include a self-made dynamic library in your project, uploading to the App Store will be rejected, and if you want to upload an app, you should choose a static library .

The static library is used to extract the core code for use by other projects.

Application of static libraries: MRC libraries (e.g. Cocos2d-iphone) are used for ARC projects, and the MRC Library can be packaged into a static library.

The process of creating a static library is explained in a simple example.

This example implements a class method for obtaining a picture.

① Create a new static library project:

② creates a new class in which a picture is read by a class method implementation.

It is important to note that in order to prevent others from using the static library when the resource conflict with the library itself, often put the library's resources into a bundle, the method is to create a new folder, Renamed as Extension. bundle, right-click on the contents of the package, put in the resources, such as the creation of Sgtool.bundle, put a picture for the capture.

The Declaration and implementation of the class is as follows, taking note of the method of getting the picture in the bundle:

  sgtool.h//  staticdemo////Created by one on  8/2/15.//  Copyright (c) soulghost. All rights reserved.//#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface Sgtool: nsobject+ (uiimage*) getImage; @end
  sgtool.m//  staticdemo////Created by one on  8/2/15.//  Copyright (c) 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 on the build phases of the project configuration to add the header files that were exposed during packaging in copy files, which are used for the user to invoke the method and view the static library contents, but. m will not be exposed.

④ in the real machine and the simulator to compile a project, and then in the Products folder to. A file right-click to select the show in Finder, you can see there is a static library for the real machine and simulator.

You only need to import the. A and include, and then drag the bundle into the project to use the static library, generally naming the three files as Lib, such as:

⑤ because the real machine and simulator architecture are different, the real machine is generally ARM7, arm64, and the simulator is x86, so the static library is different, in order to be able to make a static library both can use, using the Lipo command to merge the static library.

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, use Lipo-info <file.a> to view the schemas supported by the static library.

⑥ to distinguish between debug and release static libraries:

The default is debug, which will have complete symbolic information for easy debugging and no code optimization.

Can be changed to release, will be code optimization, does not contain complete symbolic information, the execution speed slightly improved.

Change the method as shown in:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

(102) Packaging of Static library (. a)

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.