build process - ld linker question: the --whole-archive option -
the real use of --whole-archive
linker option have seen in creating shared libraries static ones. came across makefile(s) use option when linking in house static libraries. of course causes executables unnecessarily pull in unreferenced object code. reaction this plain wrong, missing here ?
the second question have has read regarding whole-archive option couldn't quite parse. effect --whole-archive
option should used while linking static library if executable links shared library in turn has (in part) same object code static library. shared library , static library have overlap in terms of object code. using option force symbols(regardless of use) resolved in executable. supposed avoid object code duplication. confusing, if symbol refereed in program must resolved uniquely @ link time, business duplication ? (forgive me if paragraph not quite epitome of clarity)
thanks
there legitimate uses of --whole-archive
when linking executable static libraries. 1 example building c++ code, global instances "register" in constructors (warning: untested code):
main.cc
typedef void (*handler)(const char *protocol); typedef map<const char *, handler> m; m m; void register_handler(const char *protocol, handler) { m[protocol] = handler; } int main(int argc, char *argv[]) { (int = 1; < argc-1; i+= 2) { m::iterator = m.find(argv[i]); if (it != m.end()) it.second(argv[i+1]); } }
http.cc (part of libhttp.a)
class httphandler { httphandler() { register_handler("http", &handle_http); } static void handle_http(const char *) { /* whatever */ } }; httphandler h; // registers main!
note there no symbols in http.cc
main.cc
needs. if link as
g++ main.cc -lhttp
you not http handler linked main executable, , not able call handle_http()
. contrast happens when link as:
g++ main.cc -wl,--whole-archive -lhttp -wl,--no-whole-archive
the same "self registration" style possible in plain-c, e.g. __attribute__((constructor))
gnu extension.
Comments
Post a Comment