C++ 未解析的外部符号 _declspec(dllimport)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17901973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:32:23  来源:igfitidea点击:

Unresolved external symbol _declspec(dllimport)

c++dllenumsconsole-applicationdllimport

提问by Kevin Jensen Petersen

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.

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

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

错误 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 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:

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

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

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

采纳答案by YK1

You need to define the static member in your cpp file in global scope.

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

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.

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

OK - when you compile the dll - you are exporting the types. So, you need to definethe static member in .cppfile of the dll. You also need to make sure that you have enabled the definition of DIALOG_MAINMENU_EXPORTSin compiler settings. This will make sure types are exported.

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

Now, when you link the console application with the dll - you will #includedll's header and dont enable any definition of DIALOG_MAINMENU_EXPORTSin 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.

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

I hope its clear now.

我希望现在清楚了。

回答by Yochai Timmer

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

如果将类定义中的静态数据成员声明为 dllexport,则定义必须出现在同一程序中的某处(与非类外部链接一样)。

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

回答by DenVys

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)

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