IOS static library [. a and framework resolution] [ultra-detailed], ios.
1. What is a database?
Library is a way to share program code.
The library is essentially a binary format of executable code that can be loaded into memory for execution. The database is divided into static and dynamic databases.
Static libraries in iOS are in the form of. a and. framework. dynamic libraries are in the form of. dylib and. framework. Later, the. dylib dynamic library is replaced by. tbd by Apple.
Ii. What is the difference between a static library and a dynamic library?
Static Library: when the link is completely copied to the executable file, multiple redundant copies are generated when it is used for multiple times.
Dynamic library: do not copy links. When the program is running, the system dynamically loads the resources to the memory for the program to call. The system only loads the resources once. Multiple programs are shared to save memory. [Currently, only the system dynamic library is allowed for ios];
Static and dynamic libraries are relative to the compilation and runtime phases: static libraries are linked to the target code during program compilation, and do not need to be changed during program running; the dynamic library is not linked to the target code during program compilation, but is loaded only when the program is running, because the dynamic library must exist during the program running.
Summary: when the same static library is used in different programs, each program must be imported once and packaged to form a program. Dynamic libraries are not packaged in different programs. They are linked to and loaded only when the program is running (for example, the system framework such as UIKit and Foundation ), so the program size will be much smaller, but Apple will not allow it to use its own dynamic library, otherwise the review will fail.
Iii. Static library form in iOS?
. A and. framework
4. What is the dynamic library form in iOS?
. Dylib and. framework
5. Why is framework both a static library and a dynamic library?
The. framework of the system is a dynamic library, and the. framework we create ourselves is a static library.
6. What is the difference between a and. framework?
. A is a pure binary file. In the. framework, there are resource files in addition to binary files.
The. a file cannot be used directly. At least. H files must be used together. The. framework file can be used directly.
. A +. h + sourceFile =. framework.
We recommend that you use. framework.
7. Why should I use static databases?
Easy to share code and make it easy to use.
Implement modularization of iOS programs. You can modularize fixed services into static databases.
Share your code library with others, but do not want others to see the implementation of your code.
Third-party sdk development needs.
8. Notes when creating a static database:
1. Note: whether it is. a static library also. framework static library, we need binary files +. h + the format of other resource files. The difference is that ,. a itself is a binary file, and we need to configure it ourselves. h and other files can be used, while. the framework itself contains. h and other files, which can be used directly.
2. Processing of image resources: two static libraries generally place image files separately. in the bundle file. bundle name and. a or. the framework name is the same .. The bundle file is easy to understand. You can create a new folder and change it to. bundle. Right-click the file to add image resources to the package.
3category is often used in our actual development projects. It is no problem to compress category into a static library, but in the project where this static library is used, when the method in category is called, the running error (selector not recognized) of this method cannot be found. The solution is as follows: configure the value of other linker flags in the project using the static library as-ObjC.
4. If a static library is complex, you need to expose it. h. h file (generally this. the H file name is the same as the static library name), and then all the files to be exposed. H files are concentrated in this. in the H file, and those that need to be exposed. h does not need to be exposed any more. h can be exposed.
9. Create a static database
Step 1: Create a project. Generally, the project name uses the library name. For example, if I use LIB to create a static library, my project name is LIB, And the. a static library created is LIB..
Step 2: delete the. m file and retain the. h file. Generally, there is a total. h file in the static database to facilitate external import of the header file. Then import the source file to be packaged.
Step 3: Use a real machine, compile it once, and then use the simulator to compile it again. You can generate the. a file (you must first use a real machine. Otherwise, it cannot be generated ).
Step 4: The. a file generated by Xcode is not exported by default. h file. You must add it yourself.
Step 5: export the configuration of the Products static library (in fact, you do not need to set this step. If the real machine is compiled, the system will change to Releasse by default when generating and exporting [but the simulator will not (if this is not changed, you need to set Debug to NO)])
Note: If you do not change Build Configuration to Release in step 5, the static library is stored in the Debug-iphoneos and Debug-iphonesimulator folders.
We usually use the Release mode, because the program is released after the Release, so the static library is also used in the Release mode.
Step 6: synthesize the simulator architecture [Default: The simulator compilation only generates one of the corresponding architectures, and the real machine compilation will merge the two architectures]
If the value is set to YES in step 6, the compiled. a static library only contains the architecture of the current device.
For example, if we select the iPhone 5 simulator [Command + B] to compile, The. a static library compiled can only use iPhone4s ~ 5. Run the program on the simulator. Use the iPhone 5S ~ 6 plus, the x86_64 libFMDB library cannot be found.
After it is set to NO, [Command + B] NO matter which [simulator] is selected, the [386: 32-bit architecture 4S ~ 5) [x86_64: 64-bit architecture 5S ~ The current model architecture is packaged and merged.
[Note] If [Build Active Architecture Only] is not set, the armv7 and amr64 architectures are automatically merged by default. However, Apple gave up the armv7s architecture. to merge armv7s, You need to perform the following operations before compiling. (In fact, there is no need to set this)
Step 7: Merge the architecture [real machine and simulator]
Merge real machine and simulator: lipo-create static library 1.a (PATH) static library 2.a (PATH)-output new static library.
Step 8: resource package problems
1. all static library resources should be suffixed. in the bundle folder --> avoid overwriting the file with the same name as the local file, leading to an error in loading the resource file [Note: to load the bundel path]
2. During static library packaging, resource files are not packaged --> you need to manually drag them out.
I. Classic error:
Symbol not found in XX Architecture
Undefined symbols for architecture x86_64 (armv7/armv7s/amr64/i386)
Ii. Architecture Classification
1. simulator architecture: 2 types
I386: 32-bit architecture 4S ~ 5
X86_64: 64-bit architecture 5S ~ Current Model
2. Real machine architecture: 3 types
Armv7: 32-bit architecture 3GS ~ 4S
Armv7s: special architecture 5 ~ 5C (this architecture has problems, some programs become faster, and some programs become slower)
Amr64: 64-bit architecture 5S ~ Current Model
64-bit/32-bit: memory addressing is different
Iii. How to view the static Library Architecture
Find the Products folder. If the. a file is black, right-click it and open it to the Products folder.
Lipo-info in the terminal
The OS compiled by the Generic iOS Device is available in two architectures: armv7 and arm64 (excluding armv7s: special architecture)
Compiled by the iPhone 6 s simulator: x86_64
Compiled by the iPhone 4S simulator: i386
(If you do not set the Build Active Architecture Only option, the real machine will compile two types of architectures. simulator Compilation: The corresponding one)
Iv. Merging Architecture
Generally, only the first two steps are required.
1. synthesis of the simulator Architecture: Target --> Build Settings --> Build Active Architecture Only (whether to compile the current Architecture Only) --> change Debug to NO (change to NO, the simulator can directly synthesize two architectures)
2. Merge the real machine and simulator: lipo-create static library 1.a static library 2.a-output new static library.
3. * The architecture armv7s was removed by default in the xcode version update in April. This architecture is not supported.
To support the architecture, you must manually add three architectures.
V. Debug and Release versions
In general, we should release the release version.
Debug: debug version. The system also has some debugging code. This version is a little bulky and runs slowly.
Release: release a version. The system removes debugging code, decreases the size, and runs faster.
6. Whether or not to synthesize Multiple Architectures
Advantages of combining a real machine and a simulator: debugging is very convenient, but the disadvantage is that the size will increase (an architecture occupies part of the size ).
The advantage of not merging a real machine and a simulator is that it is small and the disadvantage is that debugging is a little troublesome.
VII. Resource Package Problems
1. All static library resources should be put in the folder suffixed with. bundle --> avoid File Duplication overwriting, resulting in an error in loading the resource file
2. During static library packaging, resource files are not packaged --> you need to manually drag them out.
10. Create a static framework Library
Step 1: Create a project. Generally, the name of the database is used when the project name is used.
Step 2: import the resource file to be packaged and import the. h file that requires external access to the system's recommended. h file.
Step 3: Compile the simulator on the real machine. You may need to enter the AppleID. The exported file does not contain. h.
Step 4. Export. h
Step 5: Check whether the simulator architecture has been merged. a (skip this step) (the real machine contains two, and the simulator contains one)
Step 6. Merging the architecture [see the above for details in step 5 or 6 of.]
Step 7: Merge the architecture [real machine and simulator] [note that the merged dynamic/static library should be consistent with the original name. Otherwise, an error is reported during compilation]
Step 8: change the dynamic library to a static library [dynamic by default]. The advantage is that you do not need to add a dynamic library in future use (that is, the following method)
Last step,
After the Framework is created, it is a dynamic library by default. for use, you need to set Tarteg --> General --> Embedded Binaries --> you need to add the corresponding dynamic library. [Note that if you change it to a static library during creation, this method is not required]