c++ - C/C++ : What is the difference between a statically-linked library and an object file?

标签 c++ c static linkage object-files

我知道编译时包含在可执行文件中的代码可能来自目标文件(.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/

相关文章:

C++ 数组作用域

c++ - 用 const string 和 string const 声明一个字符串有什么区别

c - 从大范围的数字中删除数字

ios - 清除结构/类中的静态变量

c++ - 何时以及如何在 C++ 中初始化静态数据?

c++ - python::boost::对象分配

C++ 互斥体和 const 正确性

c - C语言中#pragma和_Pragma()的区别

c - 在 notepad++ 和 Sublime Text 3 中打开一个简单的 .exe 文件会产生非常不同的结果

memory - 将数据存储在内存中 : Session vs Cache vs Static