Comprehensive understanding of iOS static library development

Comprehensive understanding of iOS static library development

[[148702]]

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, and there will be multiple redundant copies if it is used multiple times

  • Dynamic library: It is not copied when linking. It is dynamically loaded into memory by the system when the program is running 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. @implementation HSCalculate
  3. + (NSInteger)sumWithNum1:(NSInteger)num1 num2:(NSInteger)num2
  4. {
  5. return num1 + num2;
  6. }
  7. @end  

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

#p#

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. @interface ViewController ()
  4. @end  
  5. @implementation ViewController
  6. - ( void )viewDidLoad {
  7. [ super viewDidLoad];
  8. NSInteger result = [HSCalculate sumWithNum1: 23 num2: 25 ];
  9. NSLog(@ "result: %d" , result);
  10. }
  11. @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:

  • Good: You can debug on a real device or on a simulator during development.

  • Disadvantage: 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)

New construction test:

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

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

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

<<:  Essential for APP to go global: How to win the European and American markets

>>:  Apple releases first iOS 9.1 beta to public beta users

Recommend

The stone actually grew hair?! But the truth is scarier than you think...

In the autumn in Alaska's Glacier Bay Park, p...

Analysis on promotion of Migu Reading and Kindle check-in activities!

Some time ago, Migu Reading launched the "Ch...

Li Yanhong's prediction for 2019: Mobile phones may be phased out

"I think the next 20 years will probably be ...

How do companies operate Douyin? Just use these 5 tricks!

Nowadays, “two Weibo and one Douyin” have become ...

How long can the huge profits of auto finance last?

If selling cars is not profitable, what will the ...

Nut R1 full review: from performance to fun, a flagship phone

Hammer Technology's history of having no flag...

Event planning and promotion, how to plan and execute an event?

1. Event Planning Overview There are three keys t...