Unresolved external symbol _declspec(dllimport)

2019-03-02 04:12发布

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.

Following error message:

Error 9 error LNK2001: unresolved external symbol "__declspec(dllimport) public: static enum Dialog_MainMenu::GAME_STATES Dialog_MainMenu::CurrentGameState" (_imp?CurrentGameState@Dialog_MainMenu@@2W4GAME_STATES@1@A) C:\Users\Kevin\Desktop\c++ projects\development_testing\The Intense Adventure\Dialogs\Dialog_MainMenu.obj Dialogs

Which I kinda don't understand. This only occured when I added an enum to my prototype in my header file.

Header file:

#ifdef DIALOG_MAINMENU_EXPORTS
#define DIALOG_MAINMENU_API __declspec(dllexport) 
#else
#define DIALOG_MAINMENU_API __declspec(dllimport) 
#endif

class Dialog_MainMenu {
public:
    static DIALOG_MAINMENU_API enum GAME_STATES {
        MAINMENU, GAME, OPTIONS, CREDITS, QUIT
    };
    static DIALOG_MAINMENU_API GAME_STATES CurrentGameState;
    DIALOG_MAINMENU_API GAME_STATES GetState();
};

(Don't know if issue lies here, so I'll just add it) cpp file in general:

//Get state
Dialog_MainMenu::GAME_STATES Dialog_MainMenu::GetState() {
 // Code..
}

//Switching state
Dialog_MainMenu::CurrentGameState = Dialog_MainMenu::GAME_STATES::GAME;

I would really appreciate, any help or atleast some advice, where I can learn more about this problem.

2条回答
不美不萌又怎样
2楼-- · 2019-03-02 04:31

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.

查看更多
\"骚年 ilove
3楼-- · 2019-03-02 04:35

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;
}
查看更多
登录 后发表回答