iOS 静态库和动态库

时间:2023-01-13 16:23:19
  • 这两个东西都是编译好的二进制文件。就是用法不同而已。为什么要分为动态和静态两种库呢?先看下图:

iOS 静态库和动态库

iOS 静态库和动态库

  • 我们可以很清楚的看到:

    • 对于静态库而言,在编译链接的时候,会将静态库所有文件都添加到 目标 app 可执行文件中,并在程序运行之后,静态库app 可执行文件 一起被加载到同一块代码区中。
      • app 可执行文件: 这个目标 app 可执行文件就是 ipa解压缩后,再显示的包内容里面与app同名的文件。
    • 对于动态库而言,在编译链接的时候,只会将动态库被引用的头文件添加到目标** app 可执行文件,区别于静态库动态库** 是在程序运行的时候被添加另外一块内存区域。

    • 下面看下苹果的官方文档中有两句对动态库静态库的解释。

      • A better approach is for an app to load code into its address space when it’s actually needed, either at launch time or at runtime. The type of library that provides this flexibility is called dynamic library.
      • 动态库:可以在 运行 or 启动 的当应用真正需要的时候加载到内存中,加载到一块*独立的于 app * 的内存地址中
      • When an app is launched, the app’s code—which includes the code of the static libraries it was linked with—is loaded into the app’s address space.Applications with large executables suffer from slow launch times and large memory footprints

      • 静态库:当程序在启动的时候,会将 app 的代码(包括静态库的代码)一起在加载到 app 所处的内存地址上。相比于动态库 的方案,使用静态库将花费更多的启动时间和内存消耗。还会增加可执行文件的大小。
  • 举个