静态库的静态成员 [英] Static members of static libraries

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

问题描述

我有静态成员的静态库。此库静态链接到主应用程序和其中一个插件。看起来像在主(应用程序)和dll(插件)中初始化的静态变量。



问题:如何避免静态变量重新初始化库加载。或者可能是我缺少一些简单的东西?



详细信息



简单静态库,包含静态成员及其getter和setter:



orbhelper.h

  class ORBHelper {
static std :: string sss_;
public:
static std :: string getStr();
static void setSTR(std :: string str);
};

orbhelper.cpp

  std :: string ORBHelper :: sss_ =init; 

static std :: string ORBHelper :: getStr()
{
std :: cerr< get<< sss_.c_str()<< std :: endl;
return sss_;
}
static void ORBHelper :: setSTR(std :: string str)
{
sss_ = str;
std :: cerr<< set<< sss_.c_str()<< std :: endl;
}

此库用于main.cpp以及另一个动态库加载在主。
在main.cpp中我设置静态字符串,并在动态库函数中获取它。



在main中设置静态变量:



main.cpp

  ... 
ORBHelper :: setStr(main);
std :: cerr<< ORBHelper :: getStr()。c_str()<< std :: endl; // prints'main'
//然后加载库
...

然后在dll中得到变量值:



hwplugin.cpp

  ... 
std :: cerr<< ORBHelper :: getStr()。c_str()<< std :: endl; // print'init'instead of'main'
...

看起来像静态变量已初始化两次。第一 - 在main.cpp之前,第二 - 当动态库加载时。具有静态类的静态库链接到主应用程序和动态库。



是的,你有两个辅助类的实例。



DLL应链接到链接静态类的可执行文件,并使用它来解析助手类。不要将DLL与静态库链接,而是将其链接到可执行文件本身。


I have static library with static member. This library statically linked to main application and to one of its plugins. Looks like static variable initializing both in main (application) and in dll (plugin).

Question: How to avoid static variable reinitialization when dynamic library loading. Or may be I missing something simple?

More information:

This is simple static library, that contains static member and it's getter and setter:

orbhelper.h

class ORBHelper {
    static std::string sss_;
public:
    static std::string getStr();
    static void setSTR(std::string str);
};

orbhelper.cpp

std::string ORBHelper::sss_ = "init";

static std::string ORBHelper::getStr()
{
    std::cerr << "get " << sss_.c_str() << std::endl;
    return sss_;
}
static void ORBHelper::setSTR(std::string str)
{
    sss_ = str;
    std::cerr << "set " << sss_.c_str() << std::endl;
}

This library used in main.cpp and also in another dynamic library, that is loaded in main. In main.cpp I set the static string and in one of dynamic library function I want to get it.

Setting static variable in main:

main.cpp

...
ORBHelper::setStr("main");
std::cerr << ORBHelper::getStr().c_str() << std::endl; //prints 'main'
//then loading library
...

Then getting variable value in dll:

hwplugin.cpp

...
std::cerr << ORBHelper::getStr().c_str() << std::endl; //prints 'init' instead of 'main'
...

Looks like static variable has been initialized twice. First – before main.cpp, second – when dynamic library loaded. Static lib with static class linked both to main app and to dynamic lib.

P.S. too many words 'static' in my question, I know =)

解决方案

Yes, you have two instances of the helper class.

The DLL should be linked against the executable that links the static class in and use that to resolve the helper class. Do not link the DLL with the static library, but link it against the executable itself.

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

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