简体   繁体   English

静态库与动态库中的符号解析

[英]Symbol resolution in static vs dynamic libraries

There is a free software project that builds some static c++ libs and then links them to make a binary. 有一个免费的软件项目,它会构建一些静态c ++库,然后将它们链接以生成二进制文件。 I'd like to separate the libraries out as .so files for dynamic linking (so other projects might make use of the lib). 我想将库分离为.so文件以进行动态链接(这样其他项目可能会使用lib)。 One library builds just fine, but when I try to link it, I get "undefined reference" errors. 一个库可以很好地构建,但是当我尝试链接它时,会出现“未定义引用”错误。

Those are easy to track down and fix (the code referenced those methods in a .h file but the corresponding .cc file was not included in the Makefile compile command). 这些很容易跟踪和修复(代码在.h文件中引用了这些方法,但相应的.cc文件未包含在Makefile编译命令中)。 I am, however, wondering why, as a general matter, a library would link just fine as a static library but not as a dynamic library. 但是,我想知道为什么,作为一个一般性的问题,为什么一个库可以像静态库一样很好地链接而不能像动态库那样链接。 What are g++ and ld doing in one case but not the other? 在一种情况下g ++和ld会做什么,而在另一种情况下又不会做什么?

Thanks much. 非常感谢。

static libraries , created with ar are just a bunch of object files. 使用ar创建的静态库只是一堆目标文件。 ar is a very simple archiver. ar是一个非常简单的存档器。 There are no dependencies resolved at link time, see the man page of ar . 链接时没有解决依赖关系,请参见ar的手册页。

Shared objects on the other hand, or dynamic libraries as you call them are a very different beast. 另一方面, 共享对象或称为动态库的动态库则与众不同。 They implement the ELF binary format and have a complicated ruleset. 它们实现ELF二进制格式并具有复杂的规则集。 They also have initializing code, and some dependencies are resolved at link time. 它们还具有初始化代码,并且某些依赖项在链接时已解决。 See http://www.akkadia.org/drepper/dsohowto.pdf and http://www.akkadia.org/drepper/goodpractice.pdf for a deeper introduction. 有关更深入的介绍,请参见http://www.akkadia.org/drepper/dsohowto.pdfhttp://www.akkadia.org/drepper/goodpractice.pdf

but when I try to link it, I get "undefined reference" errors. 但是当我尝试链接它时,出现“未定义引用”错误。

Show us your link command. 向我们显示您的链接命令。 "Undefined reference" errors are generally not expected when linking a shared library, because shared libraries are allowed (by default) to have unresolved symbols. 链接共享库时,通常不会出现“未定义的引用”错误,因为共享库(默认情况下) 允许具有未解析的符号。

Or did you mean that you get "undefined reference" errors when you link the final executable against the shared library? 还是当您针对共享库链接最终的可执行文件时,就意味着出现“未定义的引用”错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM