SoFunction
Updated on 2025-04-07

The difference between ios dynamic library and static library

1. What is a library?

Library is a way to share program code, which is generally divided into static libraries and dynamic libraries.

Static library: Completely copied to the executable file during linking, and multiple redundant copies will be available after being used multiple times.

Dynamic library: No copying is done when linking, the program is dynamically loaded into memory by the system when running, for program calls, the system only loads once, and multiple programs share, saving memory.

2. The benefits of static and dynamic libraries

Benefits of using static libraries:

1. Modularity, division of labor and cooperation
2. Avoid small changes often lead to a large number of repeated compilation connections
3. It can also be reused, please note that it is not shared

Dynamic library usage has the following benefits:

1. Using dynamic libraries, you can reduce the size of the final executable file
2. Using dynamic libraries, multiple applications share the same library file in memory, saving resources

3. Using dynamic libraries, you can update dynamic library files to achieve the purpose of updating the application without recompiling and connecting the executable program.

From 1, it can be concluded that the entire application is divided into modules, teamwork, and division of labor has a relatively small impact.
It can be seen from 2 that dynamic libraries should actually be called shared libraries. In this sense, it is understandable that Apple prohibits the use of dynamic libraries in iOS development:

Because the programs on iPhone, iPodTouch, and iPad are all single-process, that is, there is only one process running at a certain moment, then you write a shared library.
-----Who to share? (When you use it, you only have one application, and the others should be suspended. Even if multiple processes can be run at the same time, can others use the things in your shared library? You are customized for your own program.)
---At present, Apple's AppStore does not support module updates, and it is impossible to update a single file (unless you write an update mechanism yourself: it has its own server to place the latest dynamic library files)

As for why Apple bans iOS development and use dynamic libraries, I guess the above two reasons

3. What are the static and dynamic libraries forms in iOS?

Static libraries: .a and .framework
Dynamic libraries: .dylib and .framework

4. Why is framework both a static library and a dynamic library?

The system's .framework is a dynamic library, and the .framework we have created ourselves is a static library.

5. What is the difference between a and .framework?

  • .a is a pure binary file. In addition to binary files, there are resource files in the .framework.
  • The .a file cannot be used directly, at least it must be accompanied by the .h file, and the .framework file can be used directly.
  • .a + .h + sourceFile = .framework。

It is recommended to use .framework.

6. Why use static libraries?

Convenient to share code and easy to use.
Implement modularization of iOS programs. Fixed services can be modularized into static libraries.
Share your code base with others, but don't want others to see the implementation of your code.
The need to develop third-party SDKs.

7. A few points to note when making static libraries:

1 Note: Whether it is the .a static library or .framework static library, what we need is the form of binary files +.h + other resource files. The difference is that .a itself is a binary file, and we need to match .h and other files ourselves to use it. .framework itself already contains .h and other files and can be used directly.

2 Processing of image resources: Two types of static libraries generally place the image file separately in a .bundle file. Generally, the name of .bundle is the same as the name of .a or .framework. The .bundle file is easy to make. Create a new folder and change it to .bundle. Right-click to display the package content and add image resources to it.

3 category is often used in our actual development projects. There is no problem in making category into a static library. However, in projects using this static library, there will be a run-time error in which the method cannot be found when calling the method in category. The solution is: configure the value of other linker flags to -ObjC in projects using static libraries.

4 If a static library is very complex and needs to be exposed, you can create a .h file inside the static library (usually the name of this .h file is the same as the name of the static library), and then concentrate all the .h files that need to be exposed in this .h file. Those .h that originally needed to be exposed do not need to be exposed anymore, just expose .h.

The above is the detailed content of the difference between the ios dynamic library and the static library. For more information about the ios dynamic library and the static library, please pay attention to my other related articles!