dynamic and staic framework of the production point and note the difference

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/denggun12345/article/details/86156040

We produce their own framework divided according to mach-o dynamic and static two kinds, dynamic nominally here is dynamic, but not really a dynamic library, only library system is the real dynamic library, the framework of our own essence on both static library (DLL because the app can more common, so contrary to the essence of the sandbox, and we do not break this mechanism as a framework developers to develop), also called embeded that is embedded in the framework .

framework of the production process much the same, and can be a resource file (picture, xib, files, etc.) packaged into the framework, but at the same workspace and project to build a way to make these two framework, there are great the difference in this trip is also a lot of pit:

Same point:

1, the same production process

2, the resource file to be added to the framework to form a bundle

   2.1 "xib file: Note that xib resources to be converted into a nib add it to the bundle, if not converted to nib is calling this project in the framework of the library will complain can not find xib, because the essence of xcode is loaded xib load nib, while the bundle file will not compile, so when adding files to be converted to bundle nib file.

xib into nib to be transformed by the command line: ibtool --errors --warnings --output-format human-readable-text --compile YHTestSevenCell.nib YHTestSevenCell.xib

difference:

The main goal of the project is different in configuration:

1 "to use embeded framework of the project, this framework needs to be added to the General Embedded Binaries, otherwise it will error (only when the real machine will be error; simulator is not being given, normal operation):

dyld: Library not loaded: @rpath/MyFramework.framework/MyFramework

  Referenced from: /var/containers/Bundle/Application/FA25E818-DB80-4C09-9B97-31B7C600CD14/MainProject.app/MainProject

  Reason: image not found

The static framework do not need to add Embedded Binaries in.

2 "static framework need to add this static library Build Phases in a New Copy Files Phase

    The embeded in the framework do not need to add this

3 "to load the resource bundle file path (way) is not the same to load the same picture as an example:

  3.1》embeded framework:

        NSBundle *bundle = [NSBundle bundleForClass:[YHTestView class]];

        NSURL *url = [bundle URLForResource:@"Test" withExtension:@"bundle"];

        NSBundle *imageBundle = [NSBundle bundleWithURL:url];

        NSString *path = [imageBundle pathForResource:@"80" ofType:@"png"];

        imageView.image = [UIImage imageWithContentsOfFile:path];

 3.2 "static framework from mainundle take pictures:

        NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"YHTestFramework.framework/Test" ofType:@"bundle"];

        NSBundle *imageBundel = [NSBundle bundleWithPath:bundlePath];

        NSString *path = [imageBundel pathForResource:@"80" ofType:@"png"];

        imageView.image = [UIImage imageWithContentsOfFile:path];

4 "reference file differences nib file:

nib file control in the corresponding file has a reference (cable), embeded framework in the normal way using a direct, normal, and the same scene static framework will complain  Unknown class YHTestFiveCell in Interface Builder file.2019-01-08 15:11 : 26.798509 + 0800 YHTestProject [30029: 2529980 ] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x100889800> setValue: forUndefinedKey:]: this class is not key value coding-compliant for the key leftIMV. '

The reason given:

Because the static framework of static links, linker believe it will eliminate all unnecessary code. linker does not check xib file, so the class is referenced in xib, the OC and not referenced in the code, the linker will remove the final executable file class. This is a problem issue linker, not the frame (compile static library will have this problem (refer should be .a)). Apple built the framework of this problem does not occur, because they are dynamically loaded at runtime, dynamic libraries exist in ios device firmware it is unlikely to be deleted.

In short:

There are only referenced in xib, while in other oc code is not used in class, will be removed linker

solve:

4.1 ", so that the frame end users turn off optimization option linker by the Other Linker Flags their project (Build Setting -> Other Linker Flags) and add -ObjC in -all_load

4.2 ", in addition a code of the class reference to another class of the frame, but still add to -ObjC

  Here, I use the second method, because it is the cell of xib, called the class in other oc-generation YHTestFiveTableViewController.m, the use of it in the static library project added to -ObjC

Note: If the library has category also need to make treatment Other Link Flags

 

Here introduce variations along Other Linker Flags

C programs, experiences from C code into the executable steps of:

Source Code - "preprocessor -" compiler - "Assembler -" machine code - "Linker -" executable file

After a series of processing source file, generates .objc file, there are a lot .objc a project file, and have a variety of links between these .objc, linker to do is to link these object files and libraries used in together form a complete executable file, which is why the top 6 nothing but an error at run time during compilation. Content Other Link Flags is actually set other parameters in addition to the default parameters of the linker work.

Other Linker Flags common parameters:

1 "-ObjC: This flag tells the linker to the OC class and Category or nib defined in the library are loaded come in, app compiled becomes large, that is loaded a lot of unnecessary files results in executable files larger. But if the static library contains classes and classification only join the flag for the job; but when a static library, only classification and no class, -ObjC becomes ineffective, then the need to increase -all_load or -force_load

2 "-all_load: will force the linker to the target files are loaded in, even without objc code.

      Drawbacks: When using more than one static library file, encounter ld: duplicate symbol error, because different Curry will have the same destination file, there are two solutions to this problem: 1 "command line on the line unpacking 2" with another parameter -force_load

3》-force_load:

   This flag and -all_load is actually the same, but -force_load need to specify the path to the file to be loaded all the libraries, so only a fully loaded library files, without affecting the rest of the files loaded on demand

      

 

Guess you like

Origin blog.csdn.net/denggun12345/article/details/86156040