.lib中的C ++静态变量不初始化 [英] C++ static variable in .lib does not initialize

查看:225
本文介绍了.lib中的C ++静态变量不初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS2010中有静态库(.lib),并将它链接到我的测试项目。



lib有一个工厂,

  #define REGISTER_FACTORY(mType,my_class)\ 
class Factory ## my_class:public CAbstractFactory\
{\
public:\
工厂## my_class():CAbstractFactory(mType){} \
CBaseClass * Create()\
{return new我的课(); } \
}; \
static Factory ## my_class StaticFactory ## my_class;应该发生的是,在CAbstractFactory中,新工厂被注册的 > mtype 。但是当我检查工厂的工厂不存在。



它工作正常,当我使用DLL而不是.lib。我的猜测是链接器不包括静态变量,因为它没有被引用或静态变量甚至没有包括在库中。



如何强制链接器



我使用这样的宏:

  //注册可以创建ID = 100的CMyObject的工厂
REGISTER_FACTORY(100,CMyObject);

class CMyObject
{
};

CAbstractFactory看起来像这样:

  class CAbstractFactory {
CAbstractFactory(int id){
CFactory :: instance()。add(id,this);
}
}

然后在代码中, exe我使用:

  CBaseClass * pObject = CFactory :: instance()。 

这将给我一个新的 CMyObject 。想法是,我有很多不同的类对象,我有一个数据库包含id指定的对象类型我需要。
100 只是一个例子。



确实,我没有引用.lib直接,但我想要能够使用我的工厂创建对象



CFactory类是一个简单的类,保留所有CAbstractFactory类并将create方法委派给正确的工厂。

  CFactory& CFactory :: Instance()
{
static CFactory instance;
return instance;
}

主要问题在于我没有引用任何东西。 lib,因为它都是通过CFactory完成的。它的工作原理,如果我把它做一个DLL,并确保我添加一些参考这个DLL,以确保它加载。但是对于一个.lib,我甚至添加了一个虚函数,以确保我至少有一个引用不包括代码的其余部分。

解决方案

我有一个类似的问题,并通过设置lib项目作为主应用程序项目的依赖项,然后设置链接库依赖关系和使用库依赖关系输入为主项目的是解决它。


I have static library (.lib) in VS2010 and am linking it to my test project.

The lib has a factory that I create using the below MACRO:

#define REGISTER_FACTORY(mType, my_class) \
class Factory##my_class : public CAbstractFactory\
{\
public:\
    Factory##my_class() : CAbstractFactory(mType){}\
    CBaseClass *Create()\
    { return new my_class(); }\
};\
static Factory##my_class StaticFactory##my_class;

What is supposed to happen is that in the CAbstractFactory the new factory gets registered by mtype. But when I check the factory the factory does not exist.

It works fine when I use a DLL instead of a .lib. My guess is that the linker does not include the static variable as it is not referenced or the static variable was not even included in the library.

How can I force the linker to include all objects from the static library in my .exe.

I use the macro like this:

// Register factory that can create CMyObject with ID=100
REGISTER_FACTORY(100, CMyObject);

class CMyObject
{
};

The CAbstractFactory looks like this:

class CAbstractFactory {
    CAbstractFactory(int id) {
        CFactory::instance().add(id, this);
    }
}

Then some where else in the code, my main .exe I use:

CBaseClass *pObject = CFactory::instance().Create(100);

This will then give my a new CMyObject. The idea is that I have many different kind object and I have a database containing the id specifying the kind of objects I need. 100 is just an example.

So indeed, I do not reference anything from the .lib directly but I want to be able to create the objects using my factory

The CFactory class is a simple class that keeps a register (in a map) of all the CAbstractFactory classes and delegates the create method to the correct factory.

CFactory &CFactory::Instance()
{
    static CFactory instance;
    return instance;
}

The main problem lies in the fact that I do not reference anything from the .lib as it is all done through the CFactory. It works if I make it a DLL and make sure I add some reference to this DLL to make sure it is loaded. But for a .lib, I even added a dummy function to make sure I have at least one reference that doesn't include the rest of the code.

解决方案

I had a similar problem and solved it by setting the lib project as a dependency of the main app project and then setting 'Link Library Dependencies' and 'Use Library Dependency Inputs' to Yes for the main project.

这篇关于.lib中的C ++静态变量不初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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