One, foreword

This article is to record the packaging process of static library and dynamic library under iOS, so as to quickly check when using in the future, for their own also for everyone to learn records. It also briefly describes the difference between dynamic and static libraries.

Two, static library and dynamic library

1, the library

Libraries are written existing, mature, reusable code. A library is essentially a binary form of executable code that can be loaded into memory by the operating system for execution. There are two kinds of libraries: static libraries and dynamic libraries.

2. The difference between static and dynamic libraries

(1) Static libraries

The link will be copied into the executable file in its entirety, and there will be multiple copies if it is used many times. As shown in the figure below, when douyin and wechat’s executables load the static library, the static library is loaded when each executable is linked.

(2) Dynamic library

The dynamic library of the system is not the same as the dynamic library that we pack ourselves. Let’s take a look at the dynamic library of the system first.

System dynamic library: the link is not copied, the program is dynamically loaded into memory by the system when running, the system only loads once, multiple programs share, save memory. For example, when douyin and wechat’s executable files load the dynamic library, there is only one dynamic library in memory and no need to load the executable file.

Self-packaged dynamic libraries: A self-packaged dynamic library is in the application, but unlike a static library, it is not in the executable.

Third, the production of static library and dynamic library

1. Create a static library

There are two types of static libraries that we can create on iOS, namely.a and.framework.

1. Make a static library

(1) Create

First create a project of type Static Library.

(2) Compile

We can see a.a file under Products, but it still doesn’t exist, so we need to command+B to find it, because we are compiling under the emulator, so it is generated in the “debug-iphonesimulator” folder.

If a new.h file is added, it needs to be added to the header file in the Phases, where fanjiduostatic. h was added by default when creating the project.

include

2, create. Framework static library

(1) First create a project, select the Framework type.

Creating a discovery is the end of the.framework

(2) This is to set, because the default is dynamic library, to change to static library. Mach -o Type = Static Library;

Test the static library

(1) Write test classes

First, I wrote a Student class, implemented a study print method, and then I imported the Student class into the fanjiduoframeWorktest. h header file.

(2) Expose headers

Then open the header file in build Phases => Headers and drag it from the default Project to Public:

(3) Test

Set up a project named staticTestDemo and introduce it into Framwork. You can successfully call the study method!

2. Make a dynamic library

(1) Production steps

There are many kinds of dynamic libraries. Currently, we cannot package dynamic libraries like.tbd. dylib. We can only package dynamic libraries in.framework format. The system defaults to dynamic typing, and then exposes the newly added headers. The process is the same as with the.framework static library, so I won’t go over it here.

(2) Test

Create a project to test the dynamic library, drag it directly into the project, import the header files, call the methods in the dynamic library, and compile it. Error: “Reason:image not found”

Embedded
Xocde
Embedded
Xocde
Embedded
General => Frameworks, Libraries, and Embedded Content
Do Not Embed
Embed & Sign

The above is a simple introduction to static library and dynamic library, as well as the packaging process, I hope you can help when you need!

Reprint please note the original source, shall not be used for commercial dissemination – fan how much