Linux下lib详细说明

一、lib类型:
与windows下静态库(.lib)和动态库(.dll)一样,linux同样存在静态库(static library 文件后缀为.a)和共享库(shared library 文件后缀为.so),在/usr/lib目录下同时存在一个库的静态版本和动态版本。

"An archive (or static library) is simply a collection of object files stored as a single file.When you provide an archive to the linker, the linker searches the archive for the object files it needs, extracts them, and links them into your program much as if you had provided thoseobject files directly."

"A shared library is similar to a archive in that it is a grouping of object files. However, there are many important differences.The most fundamental difference is that when a shared library islinked into a program, the final executable does not actually contain the code that ispresent in the shared library. Instead, the executable merely contains a reference to theshared library."

"the object files that compose the shared library are combined into asingle object file so that a program that links against a shared library always includes allof the code in the library, rather than just those portions that are needed."

以上引自《Advanced Linux Programming》

由此可知,静态库和共享库都是一个obj文件的集合,但静态链接后,执行程序中存在自己所需obj的一份拷贝,而动态链接后,执行程序仅仅是包含对共享库的一个引用。共享库相当于一个由多个obj文件组合而成的obj文件,在链接后其所有代码被加载,不管需要的还是不需要的。

似乎可以得出一个结论:
静态链接后的程序比动态链接的所用存储空间大,因为执行程序中包含了库中代码拷贝;而动态链接的程序比静态链接的所用的运行空间大,因为它将不需要的代码也加载到运行空间。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzwfzx.html