Static Library and Dynamic library

Source: Internet
Author: User

1. What is a library and why use a library?

Library is the way to share program code, generally divided into static library and dynamic library, the library realizes the modularization of the iOS program, and makes it easy to share and use some specific function modules into the library format.

2. What are the characteristics of static libraries and dynamic libraries?

Similarities and differences point:

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 only loaded once, multiple programs can be shared, save memory.

Common:

Both static and dynamic libraries are closed-source libraries that can only be used to satisfy a feature and do not expose internal code-specific information, while third-party libraries downloaded from GitHub are mostly open source libraries

3. What are the file formats for both libraries?

Static libraries:. A and. Framework

Dynamic libraries:. Dylib and. Framework (the framework provided directly to us by the system is a dynamic library!) )

Note: 1. Two libraries have the framework format, but do they look the same?

Static Library Framework Dynamic Library framework

2. When you create a framework file, the system "default" is a dynamic library format, if you want to make a static library, you need to set the Mach-o type option in buildsetting to the static library on the line!

4. The difference between a file and a. Framework file:

The. A file is a pure binary file and cannot be used directly , it needs to be used together with the header file and the resource file.

When you package a static library, you can only package code resources, but the picture files, local JSON files, and resource files such as Xib cannot be packaged in, using. A static library requires three components:. a file + header file + resource file that needs to be exposed;

There are other resource files inside the. framework file that have binaries (such as black files) (equivalent to:. framwork file = Black binary <.a file +.h file >+ resource Files < pictures, and local Html5,json, Plist, etc.), can be directly used in the project .

Interior of the framework file: Black binary main file + various resource files

5. Some points to note when making a static library:

(1) Image resources processing: Two forms of static library, 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 file is very good, create a new folder on the desktop, rename it to Xxx.bundle (select File-Right---Display package content, drag and drop to add picture resources).

(2) category is often used in our actual development projects, the category into a static library is no problem, but in the use of this static library in the project, when calling the method in the category, there will be no runtime error finding the method: Selector not Recognized, the workaround is to configure the value of other linkerflags in a project that uses a static library as-OBJC.

(3) 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.

The main role of 6.framework dynamic libraries:

The framework was originally an apple-specific, internal-provided dynamic library file format, but since 2014 WWDC, developers have been able to customize the framework for dynamic updates (bypassing Apple Store audits, releasing updated versions from the server), This Apple-limited app must be in conflict with the Apple Store review system, so apps with a custom framework won't be available in the store, but if you're developing an in-house app, Consider an attempt to use Dynamic Update technology to integrate multiple standalone apps or function modules into one app! (I developed the app used within the enterprise, we developed the Enterprise's official website into 4 separate apps, and then transformed it into a framework file that was eventually integrated into a platform-level app to use, so you can use all the features of the original 4 app on one app!) )

7.iOS How to use the Framework for dynamic updates!

Important Reference Documents (be sure to see):

http://blog.csdn.net/like7xiaoben/article/details/44081257

Http://www.tuicool.com/articles/Ybq6Rf3

(1) FAQ Summary:

The most important point is to use the dynamic characteristics of OC to achieve the framework load start!

Note: The normal Alloc init method cannot get the class in the dynamic library, and must rely on the dynamic characteristics of nsclassfromstring and Performselector to find the class of the dynamic library and invoke the startup method inside the program!

No.2: When the same third-party libraries are referenced in the framework and main project:

When you start a dynamic library in the main project, the print table will show "CLASSXXX is implemented in both xxxandxxx.one of the two would be used. Which one is undefined. "

This is caused by the same third-party libraries or classes with the same name as the framework engineering and the main project. But it may not affect the normal operation of the program, the general practice is to add-undefined dynamic_lookup tag in the framework build Settings > Other Linker flags! The idea is to make sure that the framework is also linked to third-party libraries from the main project, but there may be some hidden problems, such as: Dynamic libraries and master projects use afnetworking for network requests, but the methods for defining HTTP requests may be different. If the dynamic library calls the main engineering method to ensure that the method in the main project can meet the use of dynamic library internal procedures, if the print station appears above the information but does not affect the normal operation of the program, it is recommended not to do too much, but rather to see the dynamic library when the launch of the selected third-party library is loaded (after I tested The framework loads the Afnetworking method in its own project rather than the method in the master project, and then modifies it.

N0.3: What is the cause of "mmap () Error 1 at address=0x100704000, size=0x00008000 segment=__text" error?

This problem is mainly due to the fact that the certificate used in the dynamic library packaging and the main program debug certificate is inconsistent, the main program is unable to open the dynamic library when the certificate is inconsistent!!

No.4:framework and master projects use a class file with the same name for errors that occur:

When you try to refer to a file that is already in the main project in the framework and add the appropriate directory through build Settings > Header Search paths, Xcode succeeds when compiling (because-undefined is added dynamic_ Lookup), and the Debug version is functioning correctly, but the release version dynamically loads with a hint that the symbol cannot be found: "Error domain=nscocoaerrordomain code=3588" the bundle " Yourframework "couldn ' t be loaded." (Dlopen (/var/mobile/containers/bundle/application/5691fb75-408a-4d9a-9347-bc7b90d343c1/yourapp.app/ Yourframework.framework "

This is because the debug version exposes symbols for all custom classes for debugging purposes, so your framework can find the appropriate symbols, while the release version does not. The only way to think of this is to copy the same file into the framework project and change the class name. This, as long as in the normal development of the custom to define the class name when the habit of adding a prefix is basically avoided.

No.5: How do I access resource files in the framework?

Using storyboard/xib created in the framework, you have direct access to the picture resources in the framework!

But the framework in the "Imagenamed:" Way to load the photos will be lost!!! This is because the Imagenamed method defaults to finding resources from the Mainbundle, and the photos in the framework are loaded from within the framework, which is not a mainbudle bundle, But the framework package that exists in the main program's Docment file, the picture loading path has changed, naturally cannot find the picture resource, so need to modify the method of loading the picture! (This is a very painful problem, which means that the imagenamed method of loading the image in the original project needs to be changed to the method of loading the picture resource from the specified bundle.) I wrote a uiimage in the framework, "imagefrombundlenamed" replaces all the "imagenamed" methods in the framework to load a picture resource.

No.6 loading the framework in the main project may appear the pop-up window display position is abnormal!

This problem is very easy to see, because opening the framework in the main project to jump from one app to another app,framwork the pop-up window is not managed by the Navigationcontroller of the main project, which is likely to cause an error in the pop-up display location.

The workaround is: 1. Replace the Keywindow loaded by the framework with the Lastwindow (Keywindow is the active window, and Lastwindow is the topmost window); 2. Framwork through Navigationcont Roller.view gets the current view of the pop-up display (the view is also bound to be the most front-end page in the framework), then do the corresponding action!

No.7 in the framework, picture resources cannot be managed with Xcassets!

This is because the Xcassets file in the framework becomes a. cer file after it is packaged and does not expose the picture resource, so the framework cannot manage the picture resource through Xcassets.

The workaround is to create a resource folder to load all the picture files in to manage them.

No.8 picture naming problem:

The same picture contains a variety of adaptation dimensions, you must ensure that the image name is consistent, and the @2x/@3x end, case-sensitive. When we call the picture, we only use the previous image name of the @ symbol, because the system will automatically choose to load the picture of @2x or @3x under the image according to the different screen of the phone!

No.9 schema Error:

Error description: "Dlopen (/path/to/framework,9): no suitable image found. Didfind:/path/to/framework:mach-o, but wrong architecture "

Workaround: When packaging the framework, it is important to distinguish between supporting the iphone or Iphonesimulayor, if the package type is wrong, you cannot start the framework file on the appropriate device properly; It is best to merge the real and emulator packages via the Lipo Merge command, so you don't have to worry about the problem that caused the schema error!

(1) View the schema information supported by the framework package via terminal commands: Lipo-info (Drag the black file in the framework) to view the framework's schema information! There are four types of frameworks we can pack:

The black file is the file to be merged

(2) Merger command: lipo-create XXX xxx-output newName

"XXX" is the path information that is generated when the black files of the simulator package and the real package are dragged to the terminal;

"NewName" is the name of the merged custom, but I usually drag the path of the real package to replace the name, so that the merged file is directly included in the real machine package, and then used.

N0.10 Signature Issue:

When the system loads the dynamic library, it checks the framework's signature, which must contain teamidentifier and that the framework and the master project's teamidentifier must be consistent.

If inconsistent, the following "Mmap ()" Error is reported:

Error Loading/path/to/framework:dlopen (/path/to/framework,265): no suitable image found. Didfind:/path/to/framework:mmap () Error1

No.11: about other linker flag Setup issues:

The use of dynamic libraries is very prone to link errors, and most of the cases that occur in the loading framework of the category! The root cause is that the OBJECTIVE-C linker does not create a symbol table for each method, but rather simply establishes a symbol table for the class. In this case, if the static library defines the classification of a class that already exists, the linker will assume that the class already exists and will not combine the classification with the code of the core class. In this case, in the final executable, the code in the taxonomy is missing, so the function call fails. The common setup method is to add a statement to the other linker flag:-all_load, but this is not omnipotent, but to a certain extent solved the link problem.

About Flag Callout Information: The difference between-all load and-OBJC Please refer to the link: http://my.oschina.net/u/728866/blog/194741

Note: When the flag is added to the comment but it is still unusable, it may be reported that flag and Bitcode conflict issues, especially the third-party library May and Bitcode conflict, so you need to set Bitcode to no!

Bitcode the specific role of not to do in detail, can refer to: http://www.jianshu.com/p/3e1b4e2d06c6

The following questions are my copy of the original information, I did not encounter errors in the test, the right for reference:

N0.12: If the certificate used to package is generated prior to the release of IOS 8, there will be no teamidentifier when the package is validated. The following error is reported when the framework is loaded:

[Deny-mmap]mappedfilehasno Team identifierandisnotaplatformbinary:/private/var/mobile/containers/bundle/ application/5d8fb2f7-1083-4564-94b2-0cb7dc75c9d1/yourappnamehere.app/frameworks/yourframework.framework/ Yourframework

Can be verified by the codesign command.

Codesign-dv/path/to/yourapp.app

If the certificate is too old, the result of the output is as follows:

Executable=/path/to/yourapp.app/yourappidentifier=com.company.yourappformat=bundlewithmach-o Thin (ARMV7) codedirectoryv=20100size=221748flags=0x0 (None) hashes=11079+5location=embeddedsignaturesize=4321signedtime= October 21, 2015 morning 10:18:37info.plistentries=42teamidentifier=notsetsealed resourcesversion=2rules=12files= 2451Internal requirementscount=1size=188

Note the Teamidentifier=not set.

This is also the case when using Swift to load libswiftcore.dylib this dynamic library, and Apple's official explanation is:

To correct the problem, you'll need to sign your app using code

Signing certificates with the Subject organizational Unit (OU) set to

Your Team ID. All Enterprise and standard IOS developer certificates

That is created after IOS 8 is released has the new Team ID field in

The proper place-to-allow Swift language apps to run.

If you is an in-house Enterprise developer you'll need to be

Careful that you don't revoke a distribution certificate that is used

To sign a app any one of your Enterprise employees is still using as

Any apps this were signed with that Enterprise distribution certificate

would stop working immediately.

This problem can only be resolved by regenerating the certificate. However, the revoke old certificate will be installed by all users, and apps packaged with that certificate won't work.

Wait, are we kneeling here?

Now the Enterprise certificate is valid for three years, when the certificate expires, its packaging application can not run, the enterprise application How to replace the certificate?

Apple provides two certificates for each account, which can take effect at the same time, so that you can use another certificate to package the publication before it expires, allowing users to upgrade to a new version.

That is, you can use a different certificate to package your app, and you can overwrite apps that are packaged with legacy certificates. See the Apple documentation for details.

Static Library and Dynamic library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.