This is an iOS question.
这是一个iOS问题。
I build a static library (a framework in iOS) which is then included in an app. The size of the result binary (500kb) is smaller than the size of the static library (6mb). How does this work? My understanding of static library is that the static library is included in the final binary
我构建了一个静态库(iOS中的框架),然后将其包含在应用程序中。结果二进制(500kb)的大小小于静态库(6mb)的大小。这个怎么用?我对静态库的理解是静态库包含在最终的二进制文件中
2 个解决方案
#1
21
Because you are not using all the functions of your library. A static library of archive type .a
is a collection of .o
object files and only the object files needed in your program are included at link time.
因为您没有使用库的所有功能。归档类型.a的静态库是.o对象文件的集合,并且链接时仅包含程序中所需的目标文件。
#2
3
Whenever you statically link an executable, the linker can go ahead and resolve all symbol names (i.e. map them to an address) since all the symbols it will know about you have provided to the linker now (in the form of .o
files and .a
libraries which are really just a collection of .o
files). If there are names that aren't there, you'll get a link error (this is different than dynamic linking where you may be able to load another library at runtime). In your case, you have extra symbols that are unreferenced by the executable. Since these symbols are known to the linker as being unused they are simply removed from the executable output. So your executable will be smaller than the input libraries in this case.
无论何时静态链接可执行文件,链接器都可以继续解析所有符号名称(即将它们映射到一个地址),因为它将知道的所有符号现在已经提供给链接器(以.o文件和.o文件的形式)。一个库,它们实际上只是.o文件的集合)。如果有不存在的名称,则会出现链接错误(这与动态链接不同,您可以在运行时加载另一个库)。在您的情况下,您有可执行文件未引用的额外符号。由于链接器将这些符号称为未使用,因此只需从可执行输出中删除它们。因此,在这种情况下,您的可执行文件将小于输入库。
#1
21
Because you are not using all the functions of your library. A static library of archive type .a
is a collection of .o
object files and only the object files needed in your program are included at link time.
因为您没有使用库的所有功能。归档类型.a的静态库是.o对象文件的集合,并且链接时仅包含程序中所需的目标文件。
#2
3
Whenever you statically link an executable, the linker can go ahead and resolve all symbol names (i.e. map them to an address) since all the symbols it will know about you have provided to the linker now (in the form of .o
files and .a
libraries which are really just a collection of .o
files). If there are names that aren't there, you'll get a link error (this is different than dynamic linking where you may be able to load another library at runtime). In your case, you have extra symbols that are unreferenced by the executable. Since these symbols are known to the linker as being unused they are simply removed from the executable output. So your executable will be smaller than the input libraries in this case.
无论何时静态链接可执行文件,链接器都可以继续解析所有符号名称(即将它们映射到一个地址),因为它将知道的所有符号现在已经提供给链接器(以.o文件和.o文件的形式)。一个库,它们实际上只是.o文件的集合)。如果有不存在的名称,则会出现链接错误(这与动态链接不同,您可以在运行时加载另一个库)。在您的情况下,您有可执行文件未引用的额外符号。由于链接器将这些符号称为未使用,因此只需从可执行输出中删除它们。因此,在这种情况下,您的可执行文件将小于输入库。