Unresolved external symbol _declspec(dllimport)
You need to define the static member in your cpp file in global scope.
Dialog_MainMenu::GAME_STATES Dialog_MainMenu::CurrentGameState;
Alternatively, you can also assign it some initial value.
Dialog_MainMenu::GAME_STATES Dialog_MainMenu::CurrentGameState = Dialog_MainMenu::GAME_STATES::GAME;
EDIT:
I've created an DLL for my Console Application in Visual Studio. In my DLL I have a Class named Dialog_MainMenu with has a *.cpp file and a *.h file.
OK - when you compile the dll - you are exporting the types. So, you need to define
the static member in .cpp
file of the dll. You also need to make sure that you have enabled the definition of DIALOG_MAINMENU_EXPORTS
in compiler settings. This will make sure types are exported.
Now, when you link the console application with the dll - you will #include
dll's header and dont enable any definition of DIALOG_MAINMENU_EXPORTS
in compiler settings (just leave the settings default). This will make the compiler understand that now you are importing the types from your dll into console application.
I hope its clear now.
There is a problem with exporting static class members:
If you declare a static data member within a class definition as dllexport, a definition must occur somewhere within the same program (as with nonclass external linkage).
But what I usually do is use an access method. Static function methods are linked fine.
//.h file
class Dialog_MainMenu {
public:
static DIALOG_MAINMENU_API enum GAME_STATES {
MAINMENU, GAME, OPTIONS, CREDITS, QUIT
};
static GAME_STATES CurrentGameState;
DIALOG_MAINMENU_API GAME_STATES GetState();
static DIALOG_MAINMENU_API GAME_STATES& GetCurrentState();
};
//.cpp file
GAME_STATES& Dialog_MainMenu ::GetCurrentState()
{
return CurrentGameState;
}
Check if you added reference to your project with .dll (It's solved my problem) Right click on project > Add > Reference > (project with your .dll)