静态lib加载相关问题 [英] Static lib loading related issue

查看:179
本文介绍了静态lib加载相关问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要对二进制文件中的libs进行版本化。对于静态库,我认为这种方法可以工作,但它不会:


LibInfo.h - 所有libinfo类的基类。在构建子代码时,在 gvLibInfo 向量中注册一个对象。




  #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




Lib1.h - 从LibInfo派生并创建一个对象 l1 。除了 getversion 返回2,Lib2.h也是一样。




< #ifndef LIB1_H
#define LIB1_H

#includeLibInfo.h

class Lib1:public LibInfo
{
public:
int getversion()
{
return 1;
}

private:
};

Lib1 l1;

#endif




em> main.cpp




  #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

当我跑步时,没有任何反应。我认为这可能是由于几个原因之一:


  1. 当时 Lib1 l1 gvLibInfo 尚未构建。

  2. 我看到某处链接器将从静态库中删除任何未使用的变量。但是,当我运行二进制 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:

LibInfo.h - Base class for all libinfo classes. Registers an object in gvLibInfo vector when a child is 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 from LibInfo and creates an object l1. Lib2.h is the same except getversion 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 it = gvLibInfo.begin(); it != 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 I run, nothing happens. I thought this maybe due to one of several reasons:

  1. At the time Lib1 l1 is made, gvLibInfo has not been constructed.
  2. 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 gvLibInfo

0000000000603270 B l1

0000000000603278 B l2

I 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 refers to an external function.

Linking does not occur with -L, but with -l, i.e. it should be :

-L/path/to/libdir -lname

If your library is :

/path/to/libdir/libname.a

这篇关于静态lib加载相关问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆