静态库如何链接到依赖项?

2022-01-11 00:00:00 c linker c++ static-libraries

假设我有 libA.例如,它依赖于 libSomething,因为 libA 的非内联方法调用 libSomething.h 中的方法这一简单事实.在这种情况下,依赖关系如何联系起来?libA 在编译时是否必须静态链接到 libSomething,或者 libA 的用户(使用 libA 的应用程序)是否需要同时链接到 libA 和 libSomething?

Say I have libA. It depends on, for example, libSomething for the simple fact that a non-inline method of libA makes a call to a method in libSomething.h. How does the dependency link up in this case? Does libA have to statically link to libSomething when it is compiled, or will a user of libA (an application using libA) need to link to both libA and libSomething?

谢谢

推荐答案

静态链接只是将整个项目(函数、常量等)复制到生成的可执行文件中.如果静态库的代码包含对某些共享库项的引用,这些引用将成为生成的可执行文件中的依赖项.如果您链接库而不是可执行文件,则同样如此.

Static linking is just copying the whole items (functions, constants, etc) into the resulting executable. If a static library's code contains references to some shared library items, these references will become dependencies in the resulting executable. The same holds if you link a library instead of executable.

这个话题 讨论它在 Linux 中是如何发生的.

This thread discusses how it happens in Linux.

相关文章