Static libraries and dynamic libraries

Source: Internet
Author: User
Tags uikit

First, Brief introduction

1. What is a library?

A library is a collection of program code, a way to share program code

2. Classification of libraries

The library can be divided into 2 types depending on the disclosure of the source code

(1) Open Source Library

Open source code, can see concrete implementation

Like Sdwebimage, afnetworking.

(2) Closed Source Library

Unfair open source code, is a compiled binary file, do not see the specific implementation

Mainly divided into: static library, dynamic library

Second, static library and dynamic library

1. The existence form of static and dynamic libraries

Static libraries:. A and. Framework

Dynamic libraries:. Dylib and. Framework

2. The difference between static and dynamic libraries in use

Static libraries: When linked, the static library is completely copied into the executable file, and multiple copies of the redundant copy are used multiple times (Figure 1)

Dynamic Library: Link is not copied, the program is dynamically loaded into memory by the system, for program calls, the system is loaded only once, multiple programs are shared, saving memory (Figure 2)

Figure 1

Figure 2

Note: If you use a self-made dynamic library in your project, you cannot upload it to AppStore

Third, the production of Static library (. a)

(1) Select "Cocoa Touch Static Library"

(2) Enter the name of the static library (take Mjrefresh as an example)

  

(3) Delete auto-generated files

  

(4) Add the source code to be included in the library

   

  

Note: Resource files are not packaged in. a files.

(5) Select the. h file that needs to be exposed, and the. m file is automatically compiled into a. a file

  

Add and select the header files that need to be exposed

  

Set (move) to public.

(6) Select the real machine device, then command+b compile, libmjrefresh.a file from red to black

This time, the program will error, because the framework contains cgfloat, which is the uikit inside, so you can include the Uikit framework in the PCH file.

  

Compile again and the. A file generated after the compilation succeeds is the static library.

(7) Select Simulator, still command+b, simulator and real machine environment. A file is separate

  

(8) Right click on "show in Finder" to view the created. a file

Description

The contents of the Debug-iphoneos folder are used on the real machine.

The contents of the Debug-iphonesimulator folder are used on the simulator.

If scheme is release mode, the resulting folder starts with release

(9) Consolidation of two documents

If you want a. A file to be used on both the real machine and the simulator, you need to merge

In the terminal input command:

Lipo-create debug-iphoneos/libmjrefresh.a debug-iphonesimulator/libmjrefresh.a-output libMJRefresh.a

Description

The blue part is a fixed instruction.

Red, purple is the real machine and the simulator. A file path

Green is the path to the synthesized. a file

Generated files (static libraries)

      

Tips:

1) file size. A file's volume (in general)

For real machines. a > simulator for. A

Synthetic. A = = True machine used. A + simulator. A

2). Type of a

The Lipo–info libmjrefresh.a can be used to view the type of. A (simulator or real machine)

  

(10) How to use. A

Drag the. A,. h, resource files directly into other projects

Others get the frame, only. A files and exposed header files

  

Note: If the library contains some resource files (slices, etc.), the resource files should also be placed in the folder above.

Four, the attention point of making the static library

(1) Note:

Either the. A static library or the. Framework static Library, the final need is: binary files +. H + Other resource files

(2). A and. Framework usage Differences

. A itself is a binary file that needs to be equipped with. h and other resource files to use

The framework itself already contains. h and other resource files that you can use directly

(3) Processing of image resources

If the static library uses the picture resources, usually put in a bundle file, the bundle name is generally the same as the. A or. Framework names

Bundle creation: Create a new folder, modify the extension to. bundle, right-click the bundle file, display the package content, you can put things in the bundle file

Recommendation: Your own production of the static library to use the picture resources, do not recommend directly to the PNG suffix to use in the project, but it is recommended to put in the bundle file. This avoids conflicts between the picture name of the static library and the picture that exists in the project that uses the static library.

1) Create a new folder and put the picture of the resource you want to pack

    

2) Modify the extension to. bundle, hit Enter, click Add.

    

(4) Multi-file processing

If the static library needs to be exposed. h is more, consider creating a primary header file (typically the same name as the primary header file and the static library)

Include all other. h files that need to be exposed in the main header file

When using a static library, you only need to #import the primary header file

In fact Apple is doing this, for example: #import <UIKit/UIKit.h>

(5). Why is the framework both a static library and a dynamic library

The. Framework of a system is a dynamic library

We built it ourselves. The framework is a static library

(6) The static Library contains category (category)

If the static library contains category, sometimes the "method cannot find" error is reported in a project that uses a static library (unrecognized selector sent to instance)

Solution: Configure other Linker flags as-OBJC in a project that uses a static library

Static libraries and dynamic libraries

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.