简体   繁体   中英

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. I'd like to separate the libraries out as .so files for dynamic linking (so other projects might make use of the 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). 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?

Thanks much.

static libraries , created with ar are just a bunch of object files. ar is a very simple archiver. There are no dependencies resolved at link time, see the man page of 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. 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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