Use of static libraries in iOS

Source: Internet
Author: User

Use of static libraries in iOS
Concept library of the Library in iOS: it is a piece of compiled binary code, and the header file can be used by others.

Classification of libraries: Open Source libraries and closed source libraries

Open-source libraries such as AFNetworking are generally placed in a version control library.
Many people directly download the open source code, copy the relevant files to their own projects, and use them directly. The disadvantage is that after the version of the open source library is updated, manually copy the files once, it is troublesome to use the submodule of git. Currently, cocoapods is commonly used. You can use commands to import open-source libraries without worrying about the dependencies of open-source libraries. The essence of cocoapods is to compile the public library into a static library, and then enable the winner project to rely on this static library to close the source library usage: some code needs to be used by others, however, if you do not want to see the source code, you need to encapsulate it in the form of a library. Instead, you only expose the code that does not make major changes to the files in the early stage. We want to reduce the Compilation Time, you can package it into a library. Because the library is a compiled binary file, you can Link it during compilation without wasting Compilation Time.

Based on the Library Link Method, closed source databases can be divided into static databases and dynamic databases.

Static library :(. a or. framework) is directly copied to the target program during compilation. After compilation, the library file does not have much effect, and the target program does not have external dependencies, it can be run directly, but the disadvantage is that (the volume of the Target Program increases ). static library in the format of a, which is generally required for use. h. a. bundle file. Because the static library compiled during the creation of a static library project only supports a specific hardware architecture system, if you need to generate a general static library, you need to use the lipo command to merge multiple static databases. . For static libraries in the framework format, the framework is only a bundle (a folder containing files in the specified directory structure), but for XCode, such targets are true and false (Real/Fake) only when the XCode main project adds dependencies can the target dynamic library of the framework of the public library project be selected: The target program will not be stolen by copy during compilation, the target program only stores references pointing to the dynamic library. When the program runs, the dynamic library will be loaded in. It does not affect the volume of the target program, but may cause some performance loss. Apple does not allow self-made dynamic libraries to be shelved to appstoreiOS. a. Static library creation shows the lib project in XCode:
Vcbky/u5pLPMPGJyIC8 + DQo8aW1nIGFsdD0 = "UseLib" src = "http://www.bkjia.com/uploads/allimg/160409/0423033F1-1.gif" title = "\"/>

Note: Both Target Dependencies and Link Binary With Libraries of the project that references the static library need to add. a static library.

To reference a static library project, you must add the path of the Header file of the static library project in Header Search Paths (".." indicates that the Header file is returned to the upper directory)

If a classification is added to the created static library, the project that references the static library must add-ObjC in Other linker Flags.

Compile static library
Compile all files in the static library project:
Compile some files in the static library project:

Note: If you only need to package some files, you only need to package them in the static library package project. remove the Taget MemberShip check box in the m file, the file will not be packaged into the static library.


Check which files are included in the static library you have packaged by using the lipo command.

Lipo command usage

View the architectures contained in the static library

lipo -info lib.a   

Merge simulator library files and real library files

 lipo -create -output lib.a lib-armv6.a lib-i386.a  

Decompress the static library of the specified Architecture

Lipo-extract_family armv7-output lib-armv7.a lib. a or lipo lib. a-thin armv7-output lib-armv7.a

Decompress the static library in the format into an o file to decompress all. o files.

ar -x lib.a

Merge o files into a files

libtool -static -o lib.a *.o
Note in the static library: The dylib or framework on which the static library depends. The program that uses the static library also needs to reference the static library as binary code to differentiate the processor type, you can use lipo-create-output to create a multi-processor static library. The open source code used in the static library is the same as the open source code used in the project that references the static library.
Solution 1: Do not package the relevant open source code for the static library, and list the dependent open source libraries and their versions in the use instructions of the static library. solution 2: When the static library is packaging the open source code, modify the name of the open-source class with a prefix of more than three letters (not recommended). For a static library that has been packaged, you can use the lipo command to unpack one of the static libraries, then, conflict occurs. o file is deleted, and then the lipo command is repackaged, and then for each processor framework. file a repeat the operation, and finally merge the static library with lipo, replacing the original static library. the class names in the static library and the class names that reference the project of the static library will be treated as repeated definitions at the same time (the OC does not have a namespace)
Solution: the class name in the static library is prefixed with three or more letters and the Category is used in the static library. Even if the name is repeated, no error is returned, however, one method overwrites the other.
Solution: It is recommended that you add the prefix global variable name to the category method, which will inevitably fail to be compiled.
Solution: adding a prefix to the global variables used in the static library may enable the log when the static library is used in debug, and release closes the log, applications released to the Appstore should not print the debug logs. Therefore, when packaging static libraries, selecting Build Configuration as Release usually removes the logs of most open-source libraries, if the static library user wants to see the log during development, then the log will not be seen.
Solution: Separate the debug and release packages.

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.