Creating a static library in Xcode7
First, Brief introduction
1. What is a library?
A library is a collection of program code, a way to share program code
2. Classification of libraries
The library can be divided into 2 types depending on the disclosure of the source code
(1) Open Source Library
Open source code, can see concrete implementation
Like Sdwebimage, afnetworking.
(2) Closed Source Library
Unfair open source code, is a compiled binary file, do not see the specific implementation
Mainly divided into: static library, dynamic library
Second, static library and dynamic library
1. The existence form of static and dynamic libraries
Static libraries:. A and. Framework
Dynamic libraries:. Dylib and. Framework
2. The difference between static and dynamic libraries in use
Static libraries: When linked, the static library is completely copied into the executable file, and multiple copies of the redundant copy are used multiple times (Figure 1)
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 (Figure 2)
Three, Static library introduction:
1, the static library is divided into:
True machine-debug (Debug) version,
Real Machine-release (release) version,
Simulator-debug version,
Simulator-release version;
The release (release) version is generally packaged in development, combining the real machine and the emulator version to provide the outside world.
2, the use of the scene: in the process of project development, such as business exchange between the two companies, it is not possible to send the source code to another company, when the private content into a static library, others can only invoke the interface, and not know the implementation of the details.
Iv. creating a static library with Xcode7
. A file version (as an example of making sdwebimage static library)
1. New project, click Ios-framework&-cocoa Touch Static Library.
Name your project Sdwebimagestaticlib.
2. The system automatically generates the. h and. m files named after the project name, which can be customized to add or remove files under the directory, note that the Products folder has a. A file in red, stating that the file does not exist, What needs to be dealt with next is that all the. m files in the library file that you write yourself are hidden in the. a file.
Here we delete the system-generated. h and. m files.
3. Select T library file name under Argets , create a foreign-exposed header file
When you are finished creating
4. Import all files under Sdwebimage to Sdwebimagestaticlib
Note: The third step and the fourth step can be swapped, but after the replacement of the need to manually add the header file needs to be exposed, so do not exchange can help us to complete.
At this point, the exposed header files are under project and should be moved to public under the default conditions.
Before:
After moving:
5, then click on the upper left corner, select Edit Scheme,build configuration under Select Release, first note check whether the following release is No:yes means only compile the selected emulator device corresponding to the schema, No is to compile all emulator device support of the cup architecture (debug version of the same), select No, and then separately in the emulator and the real machine under the command+b compiled, You will see the. a file in the Products folder turns black, and this. A file is the static library that we want to get, and here's a question you'll see when you compile the emulator. A is still red, you need both the simulator and the real machine are compiled. A will become black, which should be the problem for Xcode itself.
<1>
<2>
<3>
After these 3 steps are set up, select True machine mode to compile, Command + B, and see. A static library file becomes black.
Then change the above <3> to debug, and then select the emulator to compile, Command + B, create a simulator environment, the. A file, as follows.
Can be seen in the terminal, the real machine release release mode and the simulator debug test mode. A files are present
Files are:
6. Merge the real machine and simulator. A file, enter the following command line in the terminal: Lipo-create Emulator. A path to the file. A file path-output The merged save Path (eventually a merged LIBSDWEBIMAGESTATICLIB.A file is copied together with the exposed. h header file.
7, use: Only the LIBSDWEBIMAGESTATICLIB.A and exposed. h header files are imported into the project directory for external use, so that it is very good to protect their own implementation of the source code, not to be tampered with and theft.
8. Add here:
<1>. File size. A file's volume (in general)
For real machines. a > simulator for. A
Synthetic. A = = True machine used. A + simulator. A
<2>.
Note: For support of the CPU architecture of the static library, first understand the knowledge of the CPU architecture of the iOS device, ARM is a well-known enterprise in the microprocessor industry, the ARM processor is widely used in the embedded device with the advantages of small size and high performance, and almost all mobile phones use it.
Simulator: iphone4s~5:i386 iphone5s~6plus:x86_64
Real machine: iphone3gs~4s:armv7 iphone5~5c:armv7s (Static library can run on armv7s architecture as long as ARMV7 is supported) iphone5s~6plus:arm64
ARMv6, ARMv7, armv7s is a different set of instructions for ARM CPUs, and the principle is backwards compatible. For example, the IPHONE4S CPU supports ARMV7, but it is compatible with ARMV6, but using ARMV6 instructions may not be able to fully exploit its features.
View the static library. A support for the processor architecture, first CD to the path of the. A file, command line input: Lipo-info xxxxx.a
<3> If the library also contains some resource files (slices, etc.), the resource files should also be placed in the folder above.
9, make the note point of the static library
(1) Note:
Either the. A static library or the. Framework static Library, the final need is: binary files +. H + Other resource files
(2). A and. Framework usage Differences
. A itself is a binary file that needs to be equipped with. h and other resource files to use
The framework itself already contains. h and other resource files that you can use directly
(3) Processing of image resources
If the static library uses the picture resources, usually put in a bundle file, the bundle name is generally the same as the. A or. Framework names
Bundle creation: Create a new folder, modify the extension to. bundle, right-click the bundle file, display the package content, you can put things in the bundle file
Recommendation: Your own production of the static library to use the picture resources, do not recommend directly to the PNG suffix to use in the project, but it is recommended to put in the bundle file. This avoids conflicts between the picture name of the static library and the picture that exists in the project that uses the static library.
1) Create a new folder and put the picture of the resource you want to pack
For example:
2) Modify the extension to. bundle, hit Enter, click Add.
For example:
(4) Multi-file processing
If the static library needs to be exposed. h is more, consider creating a primary header file (typically the same name as the primary header file and the static library)
Include all other. h files that need to be exposed in the main header file
When using a static library, you only need to #import the primary header file
In fact Apple is doing this, for example: #import <UIKit/UIKit.h>
(5). Why is the framework both a static library and a dynamic library
The. Framework of a system is a dynamic library
We built it ourselves. The framework is a static library
(6) The static Library contains category (category)
If the static library contains category, sometimes the "method cannot find" error is reported in a project that uses a static library (unrecognized selector sent to instance)
Solution: Configure other Linker flags as-OBJC in a project that uses a static library
Ios:xcode7 Creating a static library