我已经在 Visual Studio 中为我的控制台应用程序创建了一个 DLL。 在我的 DLL 中,我有一个名为 Dialog_MainMenu 的类,它有一个 *.cpp 文件和一个 *.h 文件。
以下错误信息:
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
我有点不明白。这仅在我向头文件中的原型(prototype)添加枚举时发生。
头文件:
#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;
如果有任何帮助或至少一些建议,我将不胜感激,我可以在其中了解有关此问题的更多信息。
最佳答案
您需要在全局范围内的 cpp 文件中定义静态成员。
Dialog_MainMenu::GAME_STATES Dialog_MainMenu::CurrentGameState;
或者,您也可以为其分配一些初始值。
Dialog_MainMenu::GAME_STATES Dialog_MainMenu::CurrentGameState = Dialog_MainMenu::GAME_STATES::GAME;
编辑:
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.
好的 - 当你编译 dll - 你正在导出类型。因此,您需要在dll 的.cpp
文件中定义
静态成员。您还需要确保在编译器设置中启用了 DIALOG_MAINMENU_EXPORTS
的定义。这将确保导出类型。
现在,当您将控制台应用程序与 dll 链接时 - 您将 #include
dll 的 header 并且不要在编译器设置中启用 DIALOG_MAINMENU_EXPORTS
的任何定义(只需保留设置默认)。这将使编译器理解您现在正在将类型从 dll 导入控制台应用程序。
希望现在天气晴朗。
关于c++ - 未解析的外部符号 _declspec(dllimport),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17901973/