静态成员变量导出错误 [英] A static member variable export error

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

问题描述

静态导出错误:
test1.dll中的


xxx.h

static export error:
in test1.dll:
xxx.h

class AFX_EXT_CLASS CTest1
{
    static int num;
}



xxx.cpp




xxx.cpp

int CTest1::num = 0;



在test2.dll的cpp中:test2依赖于test1.dll


in a cpp of test2.dll: test2 is dependent test1.dll

...
int i = CTest1::num;
...



在app:app中依赖于test1.dll& test2.dll


in a cpp of App: App is dependent test1.dll & test2.dll

...
int i = CTest1::num;
...





在test2.dll中,静态上的未解析的外部符号:CTest1 :: NUM。但在App中,没问题。

BTW:如果我使用CTest1的其他功能,没问题。



In test2.dll, "Unresolved external symbol" on static: CTest1::num. but in App, no problem.
BTW:if i use other function of CTest1, no problem.

推荐答案

可能你必须要有您的可执行文件中使用DLL的声明,如下所示:

Probably you have to have a declaration in your executable which uses DLL, like this:
declspec( __dllimport ) extern int MyClass:: num;



如果没有帮助,则添加一个静态辅助函数来访问该变量,例如:


If it does not help, then add a static helper function for accessing that variable, for example:

AFX_EXT_CLASS class CTest1
{
    static int num;
public:
    static int & GetStaticVariable();
}
 
// in CPP file:
int & CTest1::GetStaticVariable()
{
    return myStaticVariable;
}


问题可能是因为您有一个DLL从另一个DLL导入符号,并且所有符号都被标记为导出正如du [DE]上面解释的那样。



你需要的是依赖于每个DLL的条件声明,这样它们就不会被其他DLL错误地解释错误。



修改标题看起来像这样。



The issue is likely that because you have a DLL importing symbols from another DLL, and that all of the symbols are being marked for export as du[DE] explained above.

What you need are conditional declarations dependant on each DLL, so that they won''t be mistakenly interpreted incorrectly by other DLLs.

Modify your header to look something like this.

#ifdef DLLTEST1_EXPORTS
#define DLLTEST1_API __declspec(dllexport)
#else
#define DLLTEST1_API __declspec(dllimport)
#endif

class DLLTEST1_API CTest1 {
public:
	CTest1(void);
	// TODO: add your methods here.
};





然后在Test1项目或该项目的stdafx.h中定义DLLTEST1_EXPORTS。



Then define DLLTEST1_EXPORTS in either your Test1 project or in the stdafx.h for that project.


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

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