Original URL: https://my.oschina.net/kaqijiang/blog/649632
First, what is a library?
A library is a way to share program code.
A library is essentially a binary format of executable code that can be loaded into memory for execution. There are two kinds of library, static library and dynamic library.
The static libraries in iOS have. A and. Framework two forms; The dynamic library has the. dylib and. Framework forms, and later. Dylib Dynamic library is replaced by Apple in the form of. TBD.
Second, the static library and the dynamic library of the difference?
Static Library: When the link is completely copied to the executable file, multiple copies of the redundant copy are used.
Dynamic Library: Link is not copied, the program is dynamically loaded into memory by the system, for program calls, the system is loaded only once, multiple programs are shared, saving memory. [iOS only temporarily allows the use of the system Dynamic Library];
Static libraries and dynamic libraries are relative to the compile and run time: the static library is linked to the target code when the program compiles, the program will no longer need to change the static library when the program is compiled, and the dynamic library will not be linked to the target code at compile time, but only when the program is running, because the existence of the dynamic library.
Summary: When the same static library is used in different programs, it has to be imported once in each program and packaged to form a program. While the dynamic library in different programs, packaging is not packaged in, only when the program is running, only link loading (such as the system framework such as Uikit, foundation, etc.), so the program volume is much smaller, but Apple does not allow the use of their own dynamic library, otherwise the audit will not pass.
Third, the form of static library in iOS?
. A and. Framework
Iv. Dynamic Library form in iOS?
. Dylib and. Framework
V. Why is the framework a static and dynamic library?
The. Framework of a system is a dynamic library, which we build ourselves. The framework is a static library.
What is the difference between a and. Framework?
. A is a pure binary file, and the. Framework has a resource file in addition to the binaries.
. A files cannot be used directly, at least with. h files, and. Framework files can be used directly.
. A +. H + sourcefile =. Framework.
Suggested use the. Framework.
Vii. Why use a static library?
Easy to share code for reasonable use.
Implement the modularity of iOS programs. A fixed business module can be made into a static library.
Share your code base with others, but don't want others to see your code implemented.
The need to develop a third-party SDK.
Eight, the production of static Library of several notes:
1 Note: either the. A static library or the. Framework static library, all we need is a binary file +.h+ other resource files, different,. A is itself a binary file, We need our own. h and other files to use, and. The framework itself already contains. h and other files that can be used directly.
2 Image Resources processing: two static libraries, usually the picture file is placed in a separate. bundle file, general. The name of the bundle is the same as the name of the. A or. Framework. bundle files are good, create a new folder, Rename it to. Bundle on it, right-click to display the contents of the package to which you can add a picture resource.
3category is often used in our actual development projects, it is no problem to make the category into a static library, but in the project using this static library, there will be no runtime error finding the method when calling the method in the category (selector not recognized), the workaround is to configure other linker flags with a value of-OBJC in a project that uses a static library.
4 If a static library is complex, If you need to expose the. h more, you can create an. h file inside the static library (typically the name of the. h file is the same as the name of the static library), and then put all of the. h files that need to be exposed in this. h file, and those that need to be exposed. h do not need to be exposed, just have to expose the. h.
Nine, create. A static library
The first step is to create a new project. Generally use the name of the project name, such as I use LIB to create a static library, my project name is named Lib, created. A static library is LIB.A.
The second step is to delete the. m file, keep the. h file, and the general Static library has a total. h file for easy external import of the header file. Then import the source files that need to be packaged.
The third step, first with the real machine, compiled once, and then compiled with the simulator once. You can generate a. A file (you must first use the real machine or you cannot generate it).
Fourth, the. A file that Xcode generates does not export the. h file by default. Need to add it yourself.
Fifth, export the products static library configuration (in fact, do not set this step, if the real machine compiled, the system will become the default when generating the export releasse[but the simulator will not (if you do not change this need to set debug to No)])
Note: If the build configuration is not changed to release in the fifth step, the packaged static library will be stored under "Debug-iphoneos" and "Debug-iphonesimulator" two folders.
We generally use release mode, because the program is finally released after release version, so the static library is also used in release mode.
Sixth, the architecture of the synthetic simulator "default: Simulator compilation will only generate the corresponding 1 schemas, the real-computer compilation will be composed of two architectures"
If the sixth step here, set to Yes, then the compiled. A static library contains only the schema of the current device.
For example: If we choose the iphone 5 simulator "Command+b" compiled, then the compiled. A static library can only run with the Iphone4s~5 simulator, with Iphone5s~6plus, will be reported to find x86_64 Libfmdb library.
After set to No, "command+b" regardless of which "simulator", will be "386:32-bit architecture 4S ~ 5" "x86_64:64-bit architecture 5S ~ Now model" of the architecture are packaged and merged.
Note: "True machine" does not set [Build Active Architecture only] and automatically merges the ARMV7 and AMR64 schemas by default. But the armv7s architecture was abandoned by Apple, the real confidential want to merge armv7s words need to do the following to compile. (There's no need to set this up.)
Seventh step, merging the architecture "real Machine and simulator"
Real machine and Simulator merge: lipo-create Static library 1.a (path) static library 2.a (path)-output new Static library. A
Eighth step, Resource pack issues
1. The resources of the static library should be placed in a folder with the suffix. Bundle and avoid overwriting files with local file names, causing errors in loading the resource file "Note: To load the Bundel path"
2. When the static library is packaged, the resource file is not packaged and needs to be dragged out manually
A. Classic error:
The symbol is not found on the XX schema
Undefined Symbols for Architecture x86_64 (armv7/armv7s/amr64/i386)
Two. Classification of the architecture
1. Simulator Architecture: 2 Kinds
I386:32 bit architecture 4S ~ 5
x86_64:64-bit architecture 5S ~ Current Models
2. Real-Machine architecture: 3 kinds
Armv7:32 bit architecture 3GS ~ 4S
ARMV7S: Special Architecture 5 ~ 5C (This architecture is problematic, some programs become faster, some programs become slower)
amr64:64-bit architecture 5S ~ Current Models
64-bit/32-bit: different memory addressing
Three. How to view the static library schema
Find the Products folder if the. A file is black and right-click to open the Products folder
Lipo-info in terminal
Generic IOS device compiled OS available with 2 architectures: armv7/arm64 (does not include armv7s: Special architecture)
iphone6s Simulator compiled by: x86_64
Iphone4s Simulator compiled by: i386
(Do not set build Active Architecture only in the case of a real machine compiled 2 architectures, simulator compilation: corresponding 1 architectures)
Four. Composition architecture
In general, only the first two steps can be
1. The simulator architecture is synthesized: Target--build Settings--Build active Architecture only (if only the current schema is compiled)--Debug to No (no, the simulator can be directly 2 Types of architectures)
2. Real machine and Simulator merge: lipo-create static library 1.a static library 2.a-output new Static library. A
3.* armv7s This schema, the default export of this schema was canceled in the October 2014 Xcode version update. You can not support this schema.
If you want to support it, you need to add 3 schemas manually.
Five. Debug and Release versions
In general, we should publish the release version.
Debug: Debug version, the system itself will have some debugging code. This version will be slightly larger and will run slightly slower
Release: Release version, the system will be removed debugging code, the volume becomes smaller, running faster. No obvious feelings for the user
Six. Whether or not to synthesize multiple architectures
The benefits of real-machine and simulator compositing: debugging can be very handy, and the downside is that the volume becomes larger (one architecture takes up part of the volume).
The advantages of not synthesizing the real machine and simulator: small size, the disadvantage is that debugging a little more trouble.
Seven. Issues with resource bundles
1. The resources of the static library should be placed in a folder with the suffix. Bundle and avoid overwriting files with duplicate names, causing errors in loading resource files
2. When the static library is packaged, the resource file is not packaged and needs to be dragged out manually
X. Creating a framework Static library
The first step, the new project. Use the name of the library in general with the project name
The second step is to import the resource files that need to be packaged, and import the. h files that the resource files need to access to the system recommended. h
The third step, the real machine, the simulator compiles a bit. You may need to enter AppleID. The exported file does not contain the. h
The fourth step, export. h
Fifth step, you can review the emulator architecture without merging contains a few of the same. A (you can skip this step) (the real machine contains two, the simulator contains one)
Sixth step, composite architecture "with. A five or six step closer look at the top"
Seventh, merge the schema "Real machine and simulator" Note that the dynamic/static library after merging is the same as the original name. Otherwise compile the error when using the "
Eighth step, dynamic library variable static Library "default is dynamic" the benefit is that you do not need to add a dynamic library when using the future (that is, the method below)
The last step,
After the framework is made, the default is the dynamic library. When you use it, you need to set it up: Tarteg------and Embedded Binaries--you need to add the corresponding dynamic library "Note that if you change to a static library when you make it, you don't have to do this."
Thank you.
"Go to" iOS static Library ". A and framework" "super detailed"