未解析的外部符号__declspec(dllimport)

问题描述 投票:1回答:3

我在Visual Studio中为我的控制台应用程序创建了一个DLL。在我的DLL中,我有一个名为Dialog_MainMenu的类,它有一个* .cpp文件和一个* .h文件。

以下错误消息:

错误9错误LNK2001:未解析的外部符号“__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 \ Intense Adventure \ Dialogs \ Dialog_MainMenu.obj对话框

我有点不明白。这只发生在我在头文件中添加枚举到我的原型时。

头文件:

#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();
};

(不知道问题是否在这里,所以我只是添加它)cpp文件一般:

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

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

我真的很感激,任何帮助或至少一些建议,我可以在这里了解更多关于这个问题。

c++ dll enums console-application dllimport
3个回答
4
投票

您需要在全局范围内的cpp文件中定义静态成员。

Dialog_MainMenu::GAME_STATES Dialog_MainMenu::CurrentGameState;

或者,您也可以为其分配一些初始值。

Dialog_MainMenu::GAME_STATES Dialog_MainMenu::CurrentGameState = Dialog_MainMenu::GAME_STATES::GAME;

编辑:

我在Visual Studio中为我的控制台应用程序创建了一个DLL。在我的DLL中,我有一个名为Dialog_MainMenu的类,它有一个* .cpp文件和一个* .h文件。

好的 - 当您编译dll时 - 您正在导出类型。所以,你需要define在dll的.cpp文件中的静态成员。您还需要确保在编译器设置中启用了DIALOG_MAINMENU_EXPORTS的定义。这将确保导出类型。

现在,当您将控制台应用程序与dll链接时 - 您将使用#include dll的标头,并且在编译器设置中不启用DIALOG_MAINMENU_EXPORTS的任何定义(只保留设置默认值)。这将使编译器理解现在您将类型从dll导入到控制台应用程序中。

我希望它现在清楚。


1
投票

导出静态类成员时出现问题:

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).

但我通常做的是使用访问方法。静态函数方法链接正常。

//.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;
}

0
投票

检查是否使用.dll添加了对项目的引用(它解决了我的问题)右键单击项目>添加>引用>(使用.dll项目)

© www.soinside.com 2019 - 2024. All rights reserved.