未解析的外部符号_declspec(dllimport) [英] Unresolved external symbol _declspec(dllimport)

查看:436
本文介绍了未解析的外部符号_declspec(dllimport)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

strong>:

Following error message:


错误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\ The Intense
Adventure\Dialogs\Dialog_MainMenu.obj对话框

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

哪个有点不明白。

头文件

#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文件

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

推荐答案

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

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


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

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 - 你正在导出类型。所以,你需要 define 静态成员在 .cpp 文件的dll。您还需要确保在编译器设置中启用了 DIALOG_MAINMENU_EXPORTS 的定义。

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.

现在,当您将控制台应用程序与dll关联时,您将会 #include dll的头,不要在编译器设置中启用 DIALOG_MAINMENU_EXPORTS 的任何定义(只保留默认设置)。这将使编译器理解,现在你正在从您的dll导入控制台应用程序中的类型。

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.

我希望它现在清楚。

这篇关于未解析的外部符号_declspec(dllimport)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆