IOS Static library, dynamic library and Framework

Source: Internet
Author: User
Tags uikit

IOS Static library, dynamic library and FrameworkThe difference between a static library and a dynamic library

First of all to see what is a library, the library is plainly a compiled binary code, plus a header file can be used by others.

When will we use the library? One situation is that some code needs to be used by others, but we do not want others to see the source code, it needs to be packaged in the form of a library, only exposed files. Another situation is that for some code that does not make big changes, we want to reduce the compile time, we can package it into a library, because the library is already compiled binary, compile the time only need Link, do not waste compilation time.

The above mentioned that the library in use when the need to link,link the way there are two, static and dynamic, resulting in a static library and dynamic library.

Static Library

Static libraries are static link libraries (. lib,linux and Mac under Windows. a). It is called static, because the static library will be copied directly in the compilation, copy to the target program, this code in the target program will not be changed.

The benefits of a static library are obvious, and when the compilation is complete, the library file actually doesn't work. The target program has no external dependencies and can be run directly. Of course, its shortcomings are also obvious, is to use the target program's volume increases.

Dynamic Library

The dynamic library is the dynamic-link library (. dylib under. Dll,linux under Windows. So,mac). In contrast to a static library, a dynamic library is not copied to the target program at compile time, and only references to the dynamic library are stored in the target program. When the program is running, the dynamic library is actually loaded in.

The advantage of a dynamic library is that it does not need to be copied to the target program, does not affect the volume of the target program, and the same library can be used by multiple programs (for this reason, the dynamic library is also known as a shared library). At the same time, the feature that is loaded at compile time also allows us to replace the library at any time without recompiling the code. The main problem of dynamic library is that dynamic onboarding brings some performance loss, and the use of dynamic libraries also makes the program dependent on the external environment. If the environment lacks a dynamic library or the library version is incorrect, it will cause the program to fail to run (Linux does not found error).

IOS Framework

In addition to the above mentioned. A and. Dylib, the Mac Os/ios platform can also use the Framework. The Framework is actually a packaging method that packs the library's binaries, header files, and related resource files together for easy administration and distribution.

Prior to iOS 8, the iOS platform did not support the use of dynamic frameworks, and the framework that developers could use was only Apple's own uikit.framework,foundation.framework. This limitation may be due to security considerations (see the discussion here). In other words, because IOS applications are running in the sandbox, different programs can not share code, while the dynamic download code is also banned by Apple, there is no way to play the advantages of dynamic library, in fact, there is no need for dynamic library.

Due to the limitations mentioned above, developers want to share the code on the IOS platform, the only option is to package it into a static library. A file with a header file (e.g. SDK). But this kind of packing method is not convenient, the use is also more troublesome, we still want to share the code can be like the Framework, directly thrown into the project can be used. So people came up with a variety of artifice to get Xcode to Build out the framework that IOS can use, and here and here, this approach produces the framework of the "pseudo" (Fake) framework and the "real" (real) Framewo RK the difference.

iOS 8/xcode 6, the iOS platform adds support for dynamic libraries, and Xcode 6 natively comes with Framework support (both dynamic and static), and the above mentioned artifice is not necessary (see here for a new approach). Why IOS 8 to add support for dynamic libraries? The only reason is probably the advent of Extension. Extension and apps are two separate executables and need to share code, in which case support for dynamic libraries is essential. But the dynamic Framework and the uikit.framework of the system are still very different. The framework of the system does not need to be copied to the target program, we make the framework even dynamic, and finally to be copied into the app (app and Extension Bundle is shared), so Apple again called this framework E Mbedded Framework.

Swift Support

Along with Ios8/xcode 6, we also released Swift. If you want to use external code in your project, there are only two options, one of which is to copy the code to the project, and the other is to use the dynamic Framework. Using a static library is not supported.

The main reason for this problem is that Swift's runtime is not included in the IOS system, but is packaged into the app (which is why the Swift app is large), and the static library will cause the final target program to contain a duplicate runtime (which is an Apple's own explanation). Copying the runtime at the same time will also cause problems with using the Swift library in pure OBJC projects. Apple claims that it will be added to the system when Swift's Runtime is stable, and that the restrictions will be removed (refer to the problem description of the issue and from Apple's own documentation).

CocoaPods's approach

In a pure OBJC project, CocoaPods uses the compile static library. A method integrates the code into the project. Each target in the Pods project corresponds to a static library of this Pod. However, during the compilation process, there is no real output. a file. If you need a. A file, you can refer to this here, or use the Cocoaspods-packager plugin.

When you don't want to publish your code, you can also use the framework publishing Pod,cocoapods to provide vendored_framework options for using the third-party framework, which you can refer to here and here.

For Swift projects, CocoaPods provides support for dynamic Framework, controlled by use_frameworks! options.

For more information on the extension of code distribution, refer to this blog: http://geeklu.com/2014/02/objc-lib/

Resources
    • Https://stackoverflow.com/questions/2649334/difference-between-static-and-shared-libraries
    • Https://stackoverflow.com/questions/25080914/will-ios-8-support-dynamic-linking
    • Https://stackoverflow.com/questions/6245761/difference-between-framework-and-static-library-in-xcode4-and-how-to-call-them
    • http://blog.cocoapods.org/CocoaPods-0.36/

IOS Static library, dynamic library and Framework

Related Article

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.