c++ - Static lib loading related issue -
suppose want version libs in binaries made. static libs, thought approach work not:
libinfo.h - base class libinfo classes. registers object in
gvlibinfo
vector when child constructed.
#ifndef iface_h #define iface_h #include <vector> class libinfo; extern std::vector<libinfo*> gvlibinfo; class libinfo { public: virtual int getversion() = 0; void reglib() { gvlibinfo.push_back(this); } libinfo() { reglib(); } virtual ~libinfo() {} }; #endif
lib1.h - derived libinfo , creates object
l1
. lib2.h same exceptgetversion
returns 2.
#ifndef lib1_h #define lib1_h #include "libinfo.h" class lib1 : public libinfo { public: int getversion() { return 1; } private: }; lib1 l1; #endif
main.cpp
#include "lib1.h" #include <iostream> using namespace std; vector<libinfo*> gvlibinfo; int main() { for(vector<libinfo*>::iterator = gvlibinfo.begin(); != gvlibinfo.end(); it++) { cout << (*it)->getversion() << endl; } return 0; }
compiling -
g++ -c lib1.h -o lib1.o g++ -c lib2.h -o lib2.o ar cr lib.a lib1.o lib2.o g++ main.cpp -o app -l/home/duminda/statictest/lib.a
when run, nothing happens. thought maybe due 1 of several reasons:
- at time
lib1 l1
made,gvlibinfo
has not been constructed. - i saw somewhere linker remove unused variables static lib. however, when run on binary
nm
shows :
0000000000603280 b gvlibinfo
0000000000603270 b l1
0000000000603278 b l2
i guess l1
& l2
(the corresponding object of class lib2
there 'b' flag mean? 3. else don't know.
the first answer question propose use --whole-archive flag. main difference is, instead of referring external object, library refers external function.
linking not occur -l, -l, i.e. should :
-l/path/to/libdir -lname
if library :
/path/to/libdir/libname.a
Comments
Post a Comment