Static library-.a file generation and use in iOS

Source: Internet
Author: User
Tags uikit

Recently when using a demo on the web. There is another troubling question. Is that it has nested projects in it. As shown in. There is also a project nested within the project. It's really an elaborate ...
In fact, this problem does not seem to be very difficult, if it is a general project, there are two ways to solve it:

  1. I can drag and drop the files inside the nested project. Modify some of the attributes or something that should be used.
  2. Another way is to create a static library file for all of the projects that are nested inside. Expose the. h of the file I need to use.
    But this project is more wonderful. He was c++de some papers. If you drag and drop directly, there will be a variety of references to file error problems. There is no way to use the second method now. A. A file has been generated. Although it is still working hard, at least you can see some hope!

IOS Static Library, the difference between dynamic library and framework static library and 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. 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 stabilizes, and that the restrictions will be removed.

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 the Vendored_framework option to use the third-party Framework, which you can refer to here and here.

For the Swift project, CocoaPods provides support for the dynamic Framework through the use_frameworks! Option control.

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

Transferred from: https://skyline75489.github.io/post/2015-8-14_ios_static_dynamic_framework_learning.html

How does a static library generate? introduction

In enterprise development, some core technology or common framework, for security and stability considerations, do not want to be known to the outside world, so the core code will be packaged into a static library, exposing only the header files to the programmer (such as: friends, Baidu Maps and other third-party SDK)

    • Classification of libraries-based on the disclosure of the source code, the library can be divided into 2 types
    1. Open Source Library – expose sources to see specific implementations
      Like Sdwebimage, afnetworking.
    2. Closed Source Library – Unfair open source code, is a compiled binary file, do not see the actual
      Mainly divided into: static library, dynamic library
    • The difference between a static library and a dynamic library
      Static libraries : When linked, the static library is completely copied to the executable file, and multiple copies are used multiple times.


Dynamic Library : Link is not copied, the program is dynamically loaded into memory by the system, for program calls, the system is loaded only once, multiple programs are shared, saving memory

注意:项目中如果使用了动态库,会苹果拒接– Pending verification

Version of the static library file (4 types)
    • Real Machine-debug version
    • Real Machine-release version
    • Simulator-debug Version
    • Simulator-release Version

Debug (Debug) version

1. Complete symbol information for easy commissioning
2. No optimizations are made to the code

Release (release) version

1. Complete symbolic information is not included
2. The execution code is optimized
3. The size will be slightly smaller than the debug version
4. The release version will be faster in terms of execution speed (but does not mean a significant improvement)

所以我们一般开发中都打包Release(发布)版本,提供外界

Introduction to the CPU architecture of the device

Simulator:

4s~5:i386
5s~6plus:x86_64

Real machine:

3gs~4s:armv7
5~5c:armv7s (Static library can run on armv7s architecture as long as ARMV7 is supported)
5s~6plus:arm64

Make a static library-debug version
1. New Project
2. Add a static library and name it

3. The code that needs to be packaged into a static library is placed within this folder

4. Recreate the test class (Hscalculate) to provide a method for calculating two numbers in the outside world

HSCalculate.h

1
2
3
Hscalculate:nsobject
+ (Nsinteger)num2: (Nsinteger) num2;
@end

HSCalculate.m

1
2
3
4
5
6
7
8
9
10
"HSCalculate.h"

Hscalculate

+ (Nsinteger) SumWithNum1: (nsinteger) num1 num2: (nsinteger) num2
{
return NUM1 + num2;
}

@end

5. Files that need to be exposed to the outside world (interface)

6. Package support emulator and the static library of the real machine (select the real machine and the simulator to run, will generate the corresponding static library)

7. View the packaged static library

Two folders, the. A file inside is a packaged static library

Debug-iphoneos: Real Machine
Debug-iphonesimulator: Simulator

Use the following command to view the CPU architectures supported by the static library (see the CPU schema types described above)

1
Lipo-Info xxx.a

View the CPU architectures supported by the static libraries of the packaged simulator and the real machine, respectively

You will find the static library of the simulator less 4s~5:i386 architecture

Reason:

The following debug:yes indicates that only the schema of the selected emulator is compiled, no is the CPU architecture supported by all emulators (debug Yes status changed to No)

When you are finished modifying, recompile:

8. In this project to debug the static library, ViewController.m import HSCalculate.h , test run, you will find an error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import "ViewController.h"
#import "HSCalculate.h"

@interface viewcontroller ()

@end

@implementation viewcontroller

-(void) viewdidload {
[super Viewdidload];

nsinteger result = [hscalculate sumwithnum1:23 Num2:25];
nslog (@result);

@end

Error:

Reason:

Need to import a static library (compile to run successfully)

9. Also support the static library of the real machine and simulator (need to merge)

The static library of the real machine and the simulator is not the same, can not be used in both the real machine and the simulator, but to meet this requirement, the compiled two static libraries to merge

Consolidation good or bad:

Good: You can either debug on a real machine or debug on a simulator during development
Bad: If the static library is too large, the merge package will be very large, so many third-party static libraries. A is a version-sensitive

Merging to produce a new static library:

lipo -create Debug-iphoneos/xxx.a Debug-iphonesimulator/xxx.a -output xxx.a

10. Pull the merged static library and external access files into the new project and use the (inc file)

New Project test:

Make a static library-release version

The same as the debug version, but at compile time, change the following options to

That's about the same as the steps. Well, it doesn't look like a lot of trouble. OK, I hope you're far away ... I'll definitely use it later. nn. mark--First

Reference:
Partial reprint and –ios Static library development
iOS Development Outreach-Static Library

http://rookie.org.cn/2015/12/24/StaticLibary/

Static library-.a file generation and use in iOS

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.