Static and dynamic libraries

Static libraries and dynamic inventory in form

  • Static libraries: file name extensions are. A and. Framework.
  • Dynamic libraries: file names are.tbd(formerly.dylib) and.framework.

The difference between static and dynamic libraries

  • Static library: links are copied to executable files in their entirety, and multiple uses make multiple copies.
  • Dynamic library: do not copy when linking, the program is dynamically loaded into memory by the system when running, the system only loads once, multiple programs share (such as uiKit. framework of the system), saving memory. (note: prior to iOS8, there were two ways to use library files in applications, namely. A and framework. .a is the way static libraries are, and the framework is essentially the way static libraries are, packaged as fake dynamic libraries for convenience. The real dynamic library will come after iOS8.)

The difference between.a and. Framework

A is a pure binary file, a file can not be used alone, at least with. H files, while. Framework in addition to binary files, also contains some resource files (header files, plist, etc.), because it contains header files, so. Framework can be used alone.

Image resources and UI xiB or NIB file processing

Both. A and. Framework static libraries store required images or XIB files in a bundle with the same name as. A or. Framework. It’s easy to understand why you can’t store images or XIB files in a. A file, but the. Framework is essentially a bundle, so why not create a separate bundle instead of putting images or XIB files in the. Framework? That’s because iOS doesn’t trace. Framework images and other resource files, and they don’t show up in the project, meaning that even if you put them in the. Framework directory, the system won’t scan them and therefore won’t be able to find them and use them.

The category of processing

There is no problem packaging a category as a static library, but when using a static library, a runtime selector not recognized error occurs when calling a method in a category:

  • If the third library does not have a category, Other Linker Flags need not be set
  • If the category exists in the third-party library, set it to -objc
  • If, in some versions of Xcode, problems occur, change the setting to -all_load

The operation mode

Arm7: using arm7s on oldest ios7-capable devices: using arm64 on iPhone5 and 5C: using x86_64 on i386: 32-bit emulator running on iPhone5S 64-bit ARM processor: Note: Backward compatible, ARM7 version can be done on ARM7s

A Static library production

1. New Project -> Select “Cocoa Touch Static Library”





1.png

2. Add the source code that the library needs to contain, and add the code from your project to the static library project:

3. Configuration Engineering:





2.png

Note: Build Active Architecture Only in Build Settings needs to be modified to accommodate emulators running on different CPU environments. Set this to NO. When YES, the environment under which the static library is compiled is supported.

4. Select the.h file that needs to be exposed. The.m file will be automatically compiled into the.a file:





3.png

Note: Add the “Headers” option under “Build Phases” of the target corresponding to the project. This option is not available by default. You can Add it by clicking the “Add BuildPhase” button below. There are three sub-options under this option, namely Public,Private and Project. Click the plus sign below to add the header file to the “Project”. Right-click the corresponding header file and choose “Moveto PublicGroup”. When the header file is moved to “Public”, after the Project is compiled, under the path where the Project is compiled. A “usr/local/include” folder appears with the same headers as those in the public group. In this case, just copy the. A file and the header file in this path to the required project file.

Select the real device, then Command+B compiles, and the static library file changes from red to black. Scheme can compile static libraries with different versions of DEBUG and Release, as shown in the figure below:





4.png

If you want a. A file to be used on both the real machine and the emulator, you need to merge it. On the terminal, enter the command: lipo-create Static library path 1 Static library path 2-output Static library path 3 Path 1 and 2 can be obtained by dragging and dropping the static library file. Path 3 is the saved path

The type of.a (emulator versus real) can be viewed from the lipo-info static library