Unresolved external symbol _declspec(dllimport)
Asked Answered
C

3

9

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.

Carabin answered 27/7, 2013 at 20:4 Comment(9)
did you reference the .lib file ?Korella
I sure did. I even triple checked.Carabin
Can you add the linking command used to your question?Agonic
Do you mean the Addtional Include Directiories? I don't quite understand what you mean. Im kinda new to C++.Carabin
No, I mean the full linker command that the IDE invoked to produce that linking error in your question. It should be somewhere in the buildlog.Agonic
Note, you don't need static and DIALOG_MAINMENU_API in enum GAME_STATES.Agonic
Can't seem to find that log.. :/ - Also, I've tried to remove the static and DIALOG_... Same error message.Carabin
Try looking in your project folder location. Check its subdirectories like debug, release etc.Agonic
I have a couple of files named things like link.command, link.read, etc.. Which contains nothing in it.Carabin
F
5

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.

Fakir answered 27/7, 2013 at 20:27 Comment(4)
And where is that? Can't seem to find the place, Where i've put the following line you typed at first.Carabin
Just like you declare methods in .h file and define them in .cpp - you also need to define the static members. You can put that line of code right above Dialog_MainMenu::GAME_STATES Dialog_MainMenu::GetState() {Fakir
That just give me an error: definition of dllimport static data member not allowedCarabin
are you trying to import or export types?Fakir
I
6

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)

Indecision answered 21/4, 2019 at 8:8 Comment(4)
Even 6 years later people still find this questions Probably it will be helpful for someone!Indecision
still helps todayDevisable
Yup still helpsWagonlit
Indeed, it does.Pietje
F
5

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.

Fakir answered 27/7, 2013 at 20:27 Comment(4)
And where is that? Can't seem to find the place, Where i've put the following line you typed at first.Carabin
Just like you declare methods in .h file and define them in .cpp - you also need to define the static members. You can put that line of code right above Dialog_MainMenu::GAME_STATES Dialog_MainMenu::GetState() {Fakir
That just give me an error: definition of dllimport static data member not allowedCarabin
are you trying to import or export types?Fakir
K
2

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;
}
Korella answered 27/7, 2013 at 20:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.