iOS static library development

iOS static library development

Introduction

In enterprise development, some core technologies or commonly used frameworks do not want to be known to the outside world for security and stability reasons, so the core code is packaged into a static library and only the header file is exposed to programmers (for example: third-party SDKs such as Umeng and Baidu Maps)

The existence of static libraries and dynamic libraries

Static libraries: .a and .framework
Dynamic libraries: .dylib and .framework

The difference between static libraries and dynamic libraries

Static library: When linking, the static library will be completely copied into the executable file. If it is used multiple times, there will be multiple redundant copies. Dynamic library: It is not copied when linking. When the program is running, it is dynamically loaded into the memory by the system for the program to call. The system only loads it once and multiple programs share it, saving memory.

Note: If dynamic libraries are used in the project, Apple will reject it.

Static library file versions (4 types)

  • Real machine-Debug version
  • Real machine-Release version
  • Simulator-Debug version
  • Simulator-Release version

Debug version

1. Contains complete symbol information to facilitate debugging
2. No code optimization

Release

1. Will not contain complete symbol information
2. The execution code is optimized
3. The size will be slightly smaller than the Debug version
4. In terms of execution speed, the Release version will be faster (but it does not mean there will be a significant improvement)

Therefore, we usually package the Release version in development to provide the outside world

Introduction to the device's CPU architecture (supplemental knowledge)

Simulator:

4s~5 : i386
5s~6plus : x86_64

Real machine:

3gs~4s : armv7
5~5c: armv7s (static libraries can run on armv7s architecture as long as they support armv7)
5s~6plus: arm64

Making a static library - Debug version

1. Create a new project
2. Add a static library and name it

3. The code that needs to be packaged into the static library is placed in this folder

4. Recreate the test class (HSCalculate) to provide a method for external users to calculate the sum of two numbers

HSCalculate.h

  1. @interfaceHSCalculate :NSObject
  2. + (NSInteger)sumNum1:(NSInteger)num1 num2:(NSInteger)num2;
  3. @end  

HSCalculate.m

  1. # import   "HSCalculate.h"  
  2.  
  3. @implementation HSCalculate
  4.  
  5. + (NSInteger)sumWithNum1:(NSInteger)num1 num2:(NSInteger)num2
  6. {
  7. return num1 + num2;
  8. }
  9.  
  10. @end  

5. Files (interfaces) that need to be exposed to the outside world

6. Package static libraries that support simulators and real machines (select real machines and simulators to run respectively, and the corresponding static libraries will be generated)

7. View the packaged static library

Two folders, the .a file inside is the packaged static library

Debug-iphoneos: real device
Debug-iphonesimulator: Simulator

You can use the following command to view the CPU architecture supported by the static library (you can view the CPU architecture type described above)

  1. lipo-info xxx.a

Check the CPU architectures supported by the static libraries of the packaged simulator and real machine respectively

You will find that the simulator's static library is missing 4s~5: i386 architecture

reason:

Debug: Yes means compiling only the architecture corresponding to the selected simulator, while No means compiling all the CPU architectures supported by the simulator (change the Yes status of Debug to No)

After modification, recompile:

8. In this project, debug the static library, import HSCalculate.h into ViewController.m, and run the test. You will find an error.

  1. # import   "ViewController.h"  
  2. # import   "HSCalculate.h"  
  3.  
  4. @interface ViewController ()
  5.  
  6. @end  
  7.  
  8. @implementation ViewController
  9.  
  10. - ( void )viewDidLoad {
  11. [ super viewDidLoad];
  12.  
  13. NSInteger result = [HSCalculate sumWithNum1: 23 num2: 25 ];
  14. NSLog(@ "result: %d" , result);
  15. }
  16.  
  17. @end  

Error:

reason:

Need to import static library (compile to run successfully)

9. Support static libraries for both real devices and simulators (need to be merged)

The static libraries of the real machine and the simulator are different and cannot be applied to both the real machine and the simulator at the same time. However, to meet this requirement, the two compiled static libraries must be merged.

Merge good or bad:

Pros: During development, you can debug on both the real device and the simulator. Cons: If the static library is too large, it will be very large after merging and packaging. Therefore, many third-party static libraries have .a files that distinguish between versions.

Merge to generate a new static library:

  1. lipo -create Debug-iphoneos/xxx.a Debug-iphonesimulator/xxx.a -output xxx.a

10. Pull the merged static library and externally accessed files into the new project and you can use it (created by the inc file itself)

Snip20150830_26.png

New construction test:

Snip20150830_28.png

Making a static library - Release version

The steps are the same as the Debug version, except that when compiling, change the following options


Making a static library - .framework version

The steps are basically the same as making a .a static library, but pay attention to the following points:

1. Select framework

Snip20150830_32.png

2. When compiling, it is made into a dynamic library by default. You need to select

Snip20150830_33.png

3. When merging static libraries, select the libstaticlib file in the .framework file

merge:

Delete libstaticlib, pull libCalculateTool into the libstaticlib.framework project, and the libstaticlib.framework static library is created

<<:  As a programmer, these are the ten things you should invest in most

>>:  High imitation 360 mobile phone guard

Recommend

Why don't you laugh when you tickle yourself?

© Daily Mail Leviathan Press: In fact, as early a...

Understanding Russian History in One Breath (3)

Mixed Knowledge Specially designed to cure confus...

Robin Li reveals Baidu Brain AI will become the new era of electricity

In 2016, as the dividends from mobile Internet fa...

Spring Festival Marketing Case: "I'll Treat You to New Year's Eve Dinner"

The end of 2018 is quietly approaching. After a b...

How was the underground world built?

On the one hand, modern architecture develops tow...

Why doesn’t the iPhone use USB-C but Lightning?

USB-C is becoming more and more widespread, and m...