我知道编译时包含在可执行文件中的代码可能来自目标文件(.o 文件)和静态链接库(.lib/.a 文件)。这两者在根本上和概念上有什么区别?为什么“目标代码”和“静态链接库”之间有不同的概念?各自的优点和缺点是什么,为什么要使用一个而不是另一个?静态链接库是否可以从目标文件生成,反之亦然,目标文件是否可以从静态链接库生成?
最佳答案
目标文件是已编译但未链接的代码。库包含目标文件。因此你的问题变成了,“如果我只能使用目标文件,为什么要使用静态链接库?”原因如下。
与对象集合不同,每个对象都有自己的符号表,库有一个单一的、统一的符号表,在库调用ar
时创建开发人员使用 s
开关。 s
调用 ranlib
为该存档中的所有对象创建一个统一的符号表。
在 shell 中运行 ranlib
显示在帮助文本的第一行:
Generate an index to speed access to archives.
并且来自通用 ranlib docs :
An archive with such an index speeds up linking to the library and allows routines in the library to call each other without regard to their placement in the archive. T
另见 FreeBSD ranlib docs - 不同的措辞,相同的想法:链接的速度。
关于c++ - C/C++ : What is the difference between a statically-linked library and an object file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16313919/