静态库中的所有函数是否都链接到最终的可执行文件中?
Do all functions from a static library get linked into the final executable?
我是静态库的新手,只想 100% 确定我所做的是正确的。简而言之,我将尝试用这个简单的例子来解释我的问题:
如果在mylib.a
中我定义了以下函数:
int f1 (int a, int b) {/*some code here...*/}
int f2 (int a, int b) {/*some code here...*/}
int f3 (int a, int b) {/*some code here...*/}
// we also suppose that f1 does not call f2 or f3.
在我的项目中,我链接了 mylib.a
但只使用了 f1
。 f2
和 f3
也会进入最终的可执行文件吗?
我觉得这也是特定于编译器的,但考虑到我们在这里只讨论 GCC
。我会找到关于其他编译器的任何具体信息,例如 MSVC compiler
以及他们如何处理这个有价值的问题,因为我想让我的库尽可能兼容。
我还发现维基百科上给出的解释含糊不清,因为我看不清楚 WHO 包括 those parts of the library...
。此外,表达式 it is enough to include
并没有让我相信只包含所需的代码。
With static linking, it is enough to include those parts of the library that are directly and indirectly referenced by the target executable (or target library). With dynamic libraries, the entire library is loaded, as it is not known in advance which functions will be invoked by applications.
传统上,当您 link 使用静态库时,库中满足当前未满足引用的每个目标文件都将包含在可执行文件中。库中所选对象文件的任何引用也将被拾取,重复直到没有更多的对象文件可以满足任何未满足的引用。 linking 过程然后移动到列表中的下一个库。
如果到达最后一个库的末尾时仍有未解析的引用,linker 会生成有关未定义外部引用的错误消息。
我是静态库的新手,只想 100% 确定我所做的是正确的。简而言之,我将尝试用这个简单的例子来解释我的问题:
如果在mylib.a
中我定义了以下函数:
int f1 (int a, int b) {/*some code here...*/}
int f2 (int a, int b) {/*some code here...*/}
int f3 (int a, int b) {/*some code here...*/}
// we also suppose that f1 does not call f2 or f3.
在我的项目中,我链接了 mylib.a
但只使用了 f1
。 f2
和 f3
也会进入最终的可执行文件吗?
我觉得这也是特定于编译器的,但考虑到我们在这里只讨论 GCC
。我会找到关于其他编译器的任何具体信息,例如 MSVC compiler
以及他们如何处理这个有价值的问题,因为我想让我的库尽可能兼容。
我还发现维基百科上给出的解释含糊不清,因为我看不清楚 WHO 包括 those parts of the library...
。此外,表达式 it is enough to include
并没有让我相信只包含所需的代码。
With static linking, it is enough to include those parts of the library that are directly and indirectly referenced by the target executable (or target library). With dynamic libraries, the entire library is loaded, as it is not known in advance which functions will be invoked by applications.
传统上,当您 link 使用静态库时,库中满足当前未满足引用的每个目标文件都将包含在可执行文件中。库中所选对象文件的任何引用也将被拾取,重复直到没有更多的对象文件可以满足任何未满足的引用。 linking 过程然后移动到列表中的下一个库。
如果到达最后一个库的末尾时仍有未解析的引用,linker 会生成有关未定义外部引用的错误消息。