Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved external symbol _declspec(dllimport)

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.

like image 918
Kevin Jensen Petersen Avatar asked Jul 27 '13 20:07

Kevin Jensen Petersen


2 Answers

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.

like image 81
YK1 Avatar answered Oct 10 '22 02:10

YK1


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)

like image 21
osynavets Avatar answered Oct 10 '22 01:10

osynavets