1

我有一个 DLL 和 lib 文件。我已将它们包含在根源目录中,并通过 Additional Dependencies 添加了 lib 引用。但是,我收到以下错误:

1>main.obj : error LNK2001: unresolved external symbol "class game::c_State game::state" (?state@game@@3Vc_State@1@A)
fatal error LNK1120: 1 unresolved externals

这将从“engine.h”中引用:

extern __declspec(dllexport) c_State state;

在“state.cpp”(来自 DLL 的源代码)中,它被声明为

namespace game
{
    c_State state;
    //clipped for relevance
}

可能是我需要将 DLL 放在特定的地方吗?Windows 知道去哪里看吗?我在属性中找不到专门引用 DLL 文件的地方,只有 lib 文件。

另外,在声明变量或只需要函数时,我需要一个 __declspec(dllexport) 吗?

提前致谢!

4

1 回答 1

1

您必须将 __declspec(dllexport) 应用于定义,而不是声明。此外,声明需要在另一个项目中使用 __declspec(dllimport)。所以在 .h 文件中:

#undef EXPORT
#ifdef FOO_EXPORTS
#  define EXPORT __declspec(dllexport)
#else
#  define EXPORT __declspec(dllimport)
#endif

extern EXPORT int shared;

在 DLL 源代码文件中:

__declspec(dllexport) int shared;

并在 DLL 项目中使用 Project + Properties、C/C++、Proprocessor。将 FOO_EXPORTS 添加到预处理器定义中。

于 2012-02-02T01:00:25.710 回答