Libraries are ways of sharing program code. They are generally divided into static libraries and dynamic libraries
-
Static library: full copy to executable file when linking, multiple use of multiple redundant copies. IOS static library forms:. A and. Framework
-
Dynamic library: link does not copy, program run by the system dynamically loaded into memory, for program call, the system only loaded once, multiple programs shared, saving memory. IOS dynamic library forms:.tbd(formerly.dylib) and framework
-
A is different from. Framework
-
A is a pure binary file. The. Framework has resource files in addition to binary files.
-
A files cannot be used directly, but at least a. H file must be used together. Framework files can be used directly.
-
.a +.h + sourceFile =.framework.
-
Proposed framework.
-
Create a framework
1. Create the framework
2. Change the framework type
Create a framework that defaults to a dynamic Library. Search for Mach in build Settings and change the Mach-O Type to Static Library
3. Change Build Active Architeture only to NO, specifying that only the instruction set supported by the currently connected device will be compiled. When set to YES for faster debug compilation, it will only compile the current architecture version; When set to NO, all versions are compiled, so typically DEBUG is set to YES and release is set to NO to suit different devices.
4. IOS Deployment Target, the minimum version number that the static library needs to support, must be less than or equal to the version number of the main project
5. If you want static libraries to support bitcode, search for Custom in build Settings and add -fembed-bitcode in Other C Flags
6. Use cocopods to automatically manage third-party libraries by dragging files and folders into it and putting the headers that need to be exposed in Public
7. The framework of the Run Scheme is set to Release mode, the compiler to generate true machine and simulator framework, generates two FrameworkProject. The framework file, . Then put two FrameworkProject framework in the file FrameworkProject with synthesis of a command.
-
Note: 1. Xcode12 compilation process may have the following error: The linked framework ‘XXXX. Framework’ is missing one or more architectures require by this target:armv7
Solution: Add the following code to Build Setting-Excluded Architectures :armv7 and recompileCopy the code
-
Note: 2. The original packaging script corresponds to emulator and real machine respectively. After updating Xcode12, when synthesizing into FAT package, C have the same architectures (arm64) and can’t be in the same fat output file
Solution: Add the following code to Build Setting-Excluded Architectures :arm64.Copy the code
Lipo-create The absolute path to the binaries of the first framework. The absolute path to the binaries of the second framework. -output The path where the files are stored/the SDK name of the output. Finally, replace this with any binaries in the framework, and you’re done.
8. A few points to note when making a static library:
-
Pay attention to understanding: Whether the. A static library or. Framework static library, we need to be binary files +. H + other resource files, the difference is that. A is a binary file, we need to match.
-
There is no problem with using static libraries for categories, but in projects that use static libraries, there is a runtime error when calling a method in a category (selector not recognized). Configure other Linker Flags to -objc in projects that use static libraries.
-
-ObjC/-all_load/-force_load
-
We know that in Objective-C method calls are determined at run time, so Objective-C doesn’t define a link symbol for every method, it just creates a link symbol for every class. So when you use categories in a static library to extend an existing class, the linker doesn’t know how to integrate the existing methods of the class with the methods in the category, and you’re going to get selector not recognized when you call the methods in the category. Setting the ObjC flag solves this problem by loading all the object files associated with a class. Because doing so makes the executable larger, you need to do this manually.
-
In 64-bit ios applications, the -objc flag will fail when there is only a category in the static library without a class definition (this is a linker bug). You can use -all_load to force loading of all target files or -force_load to specify loading of a package. Do not use this parameter carelessly! If you use more than one static library file and then use this parameter, you may get ld: duplicate symbol error because different libraries may have the same object file, so it is recommended to use the -force_load parameter when -objc fails.
-
-force_load does the same thing as -all_load, except that -force_load needs to specify the path of the library file to be loaded completely. In this way, you only load one library file completely without affecting the rest of the library files to be loaded on demand.