DLL的全局结构?

时间:2016-08-04 08:50:59

标签: c++ dll global

我正在研究DLL。这是我的IGlobal.h,我将其与其他人.h/.cpp多次合并:

#ifndef _IGLOBALS_
#define _IGLOBALS_

struct IGlobalBitmaps {
    IBitmap mKnobGeneral;
    IBitmap mButtonScore;
    IBitmap mButtonRandom;
    IBitmap mButtonLoad;
    IBitmap mButtonClear;
    IBitmap mButtonShape;
    IBitmap mSwitchGeneral;
};

IGlobalBitmaps gGlobalBitmaps;

#endif // !_IGLOBALS_

当我编译DLL时,它会说 LNK1169找到一个或多个多重定义的符号

我该怎么办?我不能使用const(因为一些IBitmap方法不是const)而且static都没有(因为它是一个DLL,后来变得很痛苦。)

2 个答案:

答案 0 :(得分:2)

你应该在你的.h文件中将变量声明为extern,并在任何一个cpp文件中定义它。

答案 1 :(得分:0)

在DLL中声明要从DLL外部使用的变量时,需要为其指定导入/导出状态。

#ifdef BUILDING_DLL
// When building the DLL, export
# define DECL_DLL __declspec (dllexport)

#else

// When building something that uses the DLL, import
# define DECL_DLL __declspec (dllimport)
#endif

DECL_DLL IGlobalBitmaps gGlobalBitmaps;

对于奖励积分,如果您使用LoadLibrary (...)加载DLL,而不是链接到其导入库,则可以使用GetProcAddress (...)获取DLL导出的函数和变量。

相关问题