markzzz
markzzz

Reputation: 47945

Global structs across DLL?

I'm working on a DLL. This is my IGlobal.h, which I include it many time from others .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_

when I compile the DLL, it says LNK1169 one or more multiply defined symbols found.

What can I do? I can't use const (since some IBitmap methods are not const) and neither static (since its a DLL, and it become a pain later).

Upvotes: 0

Views: 471

Answers (2)

Andon M. Coleman
Andon M. Coleman

Reputation: 43329

When declaring a variable in a DLL that you want to use from outside the DLL, you need to give it import/export status.

#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;

For bonus points, if you load the DLL with LoadLibrary (...), rather than linking to its import library, you can get DLL exported functions and variables by using GetProcAddress (...).

Upvotes: 0

Ankur
Ankur

Reputation: 205

you should declare the variable as extern in your .h file, and define it in any one of the cpp files.

Upvotes: 2

Related Questions