Ios-the creation and use of static libraries

Source: Internet
Author: User

In daily project development, whether for the purpose of business communication for two company projects or to reduce project compilation time, sometimes we will package the private content of the project into a static library, or package a few changes in the project into a static library in order to improve the efficiency of compiling, then we will learn " ios-creation and use of static libraries ":

(i) The difference between the iOS static library, the dynamic library and the framework Static library and the dynamic library:

  (1) What is a library?

Libraries (library) a straightforward point is a compiled binary code, plus a header file can be used by others, (for example: iOS in the objective-c compiled. h and. m files, packaging the static library will become. h and. a file);

 (2) What is a static library?

① static libraries are static link libraries (for example,. Lib, Mac, and Linux under Windows. a);

② Static library at compile time will be directly copied a copy, copied to the target program and this code in the target program will not be changed, I guess this is the library called "Static Library" reason;

③ the pros and cons of a static library:

1) Benefit: the Static library after the compilation is completed, the library file actually does not have the function, the target program does not have the external dependence, can run directly;

2) Disadvantages: The disadvantages of Static library will use the target program volume increase;

(3) What is a dynamic library?

① Dynamic libraries are dynamic-link libraries (for example,. dll under Windows,. Dylib under Mac, and Linux under. So);

② In contrast to the static library, the dynamic library will not be copied to the target program at compile time, the target program will only store the reference to the dynamic library, and when the program runs, the dynamic library will be really loaded in;

③ the pros and cons of a dynamic library:

1) Benefit: Do not need to copy to the target program, does not affect the volume of the target program, and the same library can be used by multiple programs (for this reason, the dynamic library is also known as a shared library); At the same time, the feature that is loaded at compile time also lets us replace the library at any time without recompiling the code;

2) Cons: Dynamic loading to bring a part of the performance loss, the use of dynamic libraries will also make the program dependent on the external environment, if the environment is missing a dynamic library or the library version is incorrect, it will cause the program can not run;

(4) When will we use the library?

① Some code snippets need to be used by others, but we do not want others to see the source code, it needs to be packaged in the form of a library, only exposed files;

② for some of the code will not make big changes, we want to reduce the compile time, we can package it into a library, because the library is already compiled binary, compile time only need Link, do not waste compilation time;

Note: The above mentioned that the library in use when the need to link,link two ways: static and dynamic, then produced a static library and dynamic library ("Siege Lion" thinking is so simple????);

(5) IOS Framework?

① In addition to the above we mentioned IOS. A and. Dylib, the Mac Os/ios platform can also use Framework,framework is actually a packaging method that packages the library's binaries, header files, and related resource files together for easy management and distribution; IOS 8 Before, the IOS platform does not support the use of dynamic framework, developers can use the framework only Apple's own uikit.framework,foundation.framework and so on;

② above this limitation may be for security reasons, in other words, because IOS applications are running in the sandbox, different programs can not share code, while the dynamic download code is also banned by Apple, there is no way to play the advantages of dynamic library, in fact, there is no need for dynamic library;

③ because of the limitations mentioned above, developers want to share the code on the IOS platform, the only option is to package it into a static library. A file with a header file attached, but this kind of packaging is not convenient, the use of more trouble, we still want to share code can be like the Framework, Throw it directly into the project and you can use it.

④ at the end of the day. iOS support for dynamic libraries: After iOS 8/xcode 6 is launched, the iOS platform adds support for dynamic libraries, while Xcode 6 natively comes with Framework support (both dynamic and static);

⑤ but said here Bo mainly tell you, iOS dynamic library and use and cherish (careful application audit was rejected????);

(ii) cut into the theme "Creation of the iOS Static library":

  (1) We first understand the version of the static library file (four kinds):

① Real Machine-debug version; ② Real Machine-release version ③ simulator-debug version ④ simulator-release version;

  (2) Debug (Debug) version features:

① contains complete symbolic information to facilitate debugging; ② does not optimize the code;

  (3) Release (release) version features:

The ① does not contain complete symbolic information;  ② execution code is optimized; ③ size will be slightly smaller than the debug version ④ in the execution speed, release version will be faster;

So we are generally in the development of packaging release (release) version of the use;

  (4) To learn more about the architecture of the iphone device CPU:

① Simulator: (4s~5:i386) (5s~6splus:x86_64)

②: (3GS~4S:ARMV7) (5~5c:armv7s) (5s~6splus:arm64) [Description: The static library as long as the support of ARMV7, you can run in the structure of armv7s];

(iii) Production of Static library-debug version:

  (1) Create Cocoa touch static Library, new project Select iOS->framework&library, Cocoa Touch static library,1:

    

  (2) Click Next to name the project like "Library" click Next, Xcode automatically created for us the library.h/.m file and the corresponding directory structure, 2 shows:

    

 (3) Next we write the function implementation code in the project's. H and. m (re-create the static library, the code is sketchy please disregard????):

1 "Library.h"2 /** Hex Turn binary*/3+ (NSString *) Getbinarybyhex: (NSString *) hex;4 5 "library.m"6+ (NSString *) Getbinarybyhex: (NSString *) Hex {7Nsmutabledictionary *hexdic =[[Nsmutabledictionary alloc] init];8     9Hexdic = [[Nsmutabledictionary alloc] Initwithcapacity: -];Ten[Hexdic SetObject:@"0000"Forkey:@"0"]; One[Hexdic SetObject:@"0001"Forkey:@"1"]; A[Hexdic SetObject:@"0010"Forkey:@"2"]; -[Hexdic SetObject:@"0011"Forkey:@"3"]; -[Hexdic SetObject:@"0100"Forkey:@"4"]; the[Hexdic SetObject:@"0101"Forkey:@"5"]; -[Hexdic SetObject:@"0110"Forkey:@"6"]; -[Hexdic SetObject:@"0111"Forkey:@"7"]; -[Hexdic SetObject:@" +"Forkey:@"8"]; +[Hexdic SetObject:@"1001"Forkey:@"9"]; -[Hexdic SetObject:@"1010"Forkey:@"A"]; +[Hexdic SetObject:@"1011"Forkey:@"B"]; A[Hexdic SetObject:@"1100"Forkey:@"C"]; at[Hexdic SetObject:@"1101"Forkey:@"D"]; -[Hexdic SetObject:@"1110"Forkey:@"E"]; -[Hexdic SetObject:@"1111"Forkey:@"F"]; -      -NSString *binarystring=[[Nsmutablestring alloc] init]; -      in      for(intI=0; I<[hex length]; i++) { - Nsrange rage; toRage.length =1; +Rage.location =i; -NSString *key =[Hex substringwithrange:rage]; thebinarystring = [NSString stringWithFormat:@"%@%@", binarystring,[nsstring stringWithFormat:@"%@", [Hexdic Objectforkey:key]]; *     } $     returnbinarystring;Panax Notoginseng}

(4) Compile the project to generate the corresponding static library. A file (note the details here):

① "liblibrary.a in the box labeled ②" Before the Project compilation (COMMAND+B) is red (after compilation is black, red indicates that a virtual file cannot be found in the project), 3 shows:

      

In the process of ② compiling, we will choose "Simulator compile" and "real Machine compile", 4 Figure 5 shows:

  (5) Right click on the project directory. A file, look at the compiled static library file 6, where the file Debug-iphoneos contains the iphone real machine required liblibrary.a static library files, The file Debug-iphonesimulator contains the liblibrary.a static library files that are required by the iphone emulator, as shown in 7;

(6) View the CPU architectures supported by the static libraries of the packaged simulator and the real machine, respectively:

In the above we introduce the architecture of the iphone device CPU, here we will check whether our packaged static library conforms to the architecture standard included in the iphone device (hint: If the static library runs to different models on the return error), open the terminal to view the static library as shown in Schema 8:

    

We found the reason 9, the following debug:yes means only compile the selected emulator corresponding to the schema, no is to compile all the emulator Supported Cup architecture (debug Yes State to No);

    

Once again, command+b the Small step (4) step above, OK,???? ...

(7) Merging static libraries:

① Why do you want to merge static libraries?

Because the real machine and emulator static library, is not the same, can not be used in both the real machine and simulator, but to meet this requirement, the compiled two static libraries to be merged in use;

② the pros and cons of merging static libraries?

1): In the development process can be in the real machine debugging, but also can be debugged on the simulator;

2) Cons: If the static library is too large, the merge package will be very large, so many third-party static libraries of. A are differentiated by version;

③ Open Terminal merge Static library (finally, it's almost successful???? ...) 10 is shown below:

      

The complete command is: lipo-create/users/apple/library/developer/xcode/deriveddata/library-bmlhmlslupltsqfkcfgmgqzducdy/ build/products/debug-iphoneos/liblibrary.a/users/apple/library/developer/xcode/deriveddata/ library-bmlhmlslupltsqfkcfgmgqzducdy/build/products/debug-iphonesimulator/liblibrary.a-output/users/apple/ DESKTOP/LIBLIBRARY.A;

(8) Use of static libraries:

Add the merged static library file (. A) and the header file (. h) to the project, call the static library, the result is as follows (the code is rough Please ignore ...):

1 "ViewController.h"2 @interfaceViewcontroller:uiviewcontroller3 4 @end5 6 "VIEWCONTROLLER.M"7 #import "ViewController.h"8 #import "Library.h"9 Ten @interfaceViewcontroller () One  A @end -  - @implementationViewcontroller the  -- (void) Viewdidload { - [Super Viewdidload]; -     //Hex Turn binary +NSLog (@"hex Turn binary:%@", [Library Getbinarybyhex:@"F"]); - } +  A the log print input results are: atUse of static libraries [1006:40288] Hex Turn binary:1111

(iv) Production of Static library-release version:

The same as the debug version, but at compile time, change the following option to 12:

  

At this point, whether the real machine or the simulator can be compiled through, normal operation, and users can only through the header file to know the excuses we provide, but do not know the implementation of the file implementation of the details, which effectively hides their core technology and confidential content;

(v) above is my understanding of the iOS static library and interpretation, I hope that we complement each other to learn from each other;

Ios-the creation and use of static 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.