本文介绍了静态lib加载相关问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 假设我要对二进制文件中的libs进行版本化。对于静态库,我认为这种方法可以工作,但它不会: #ifndef IFACE_H #define IFACE_H #include< vector> class LibInfo; extern std :: vector< LibInfo *> gvLibInfo; 类LibInfo { public: virtual int getversion()= 0; void reglib() { gvLibInfo.push_back(this); } LibInfo() { reglib(); } virtual〜LibInfo() {} }; #endif #ifndef LIB1_H #define LIB1_H #includeLibInfo.h class Lib1:public LibInfo { public: int getversion() { return 1; } private:}; Lib1 l1; #endif #include h #include< iostream> using namespace std; vector< LibInfo *> gvLibInfo; int main() { for(vector< LibInfo *> :: iterator it = gvLibInfo.begin(); it!= gvLibInfo.end(); it ++ ) { cout<< (* it) - > getversion()<< endl } return 0; } 编译 - 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 当我跑步时,没有任何反应。我认为这可能是由于几个原因之一: 当时 Lib1 l1 , gvLibInfo 尚未构建。 我看到某处链接器将从静态库中删除任何未使用的变量。但是,当我运行二进制 nm 时,会显示以下内容: 0000000000603280 B gvLibInfo 0000000000603270 B l1 0000000000603278 B l2 我猜 l1 & l2 ( Lib2 的相应对象是否存在,但是B标志是什么意思? 解决方案 问题建议使用--whole-archive标志主要的区别是,到外部对象,库将引用到外部函数。 链接不会与-L,但是与-l,即它应该是: -L / path / to / libdir -lname 如果您的库是: / path / to / libdir / libname .a Suppose I want to version the libs in binaries made. For static libs, I thought this approach would work but it does not:#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#ifndef LIB1_H#define LIB1_H#include "LibInfo.h"class Lib1 : public LibInfo{ public: int getversion() { return 1; } private:};Lib1 l1;#endif#include "Lib1.h"#include <iostream>using namespace std;vector<LibInfo*> gvLibInfo;int main(){ for(vector<LibInfo*>::iterator it = gvLibInfo.begin(); it != gvLibInfo.end(); it++) { cout << (*it)->getversion() << endl; } return 0;}Compiling - g++ -c Lib1.h -o Lib1.og++ -c Lib2.h -o Lib2.oar cr lib.a Lib1.o Lib2.og++ main.cpp -o app -L/home/duminda/statictest/lib.aWhen I run, nothing happens. I thought this maybe due to one of several reasons:At the time Lib1 l1 is made, gvLibInfo has not been constructed.I saw somewhere that the linker will remove any unused variables from a static lib. However, when I run on the binary nm it shows up this :0000000000603280 B gvLibInfo0000000000603270 B l10000000000603278 B l2I guess l1 & l2(The corresponding object of class Lib2 are there but what does that 'B' flag mean?3. Something else I don't know. 解决方案 The first answer to this question propose to use the --whole-archive flag.The main difference is, instead of referring to an external object, the library refersto an external function.Linking does not occur with -L, but with -l, i.e. it should be :-L/path/to/libdir -lnameIf your library is :/path/to/libdir/libname.a 这篇关于静态lib加载相关问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 23:54