LNK2001& LNK1120 在项目中包含 DLL 时 - MSVC++
我有一个 DLL 和 lib 文件。我已将它们包含在根源目录中,并通过其他依赖项添加了 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
which would bereferencing this from "engine.h":
extern __declspec(dllexport) c_State state;
In "state.cpp"(来自 DLL 的源代码),它被声明为
namespace game
{
c_State state;
//clipped for relevance
}
难道我需要将 DLL 放置在特定的地方? Windows 知道去哪里查找吗?我在属性中没有找到专门引用 DLL 文件的地方,只有 lib 文件。
另外,在声明变量或仅声明函数时是否需要 __declspec(dllexport) ?
提前致谢!
I have a DLL and lib file. I've included them in the root source directory and added the lib reference through Additional Dependencies. However, I get the following error:
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
which would be referencing this from "engine.h":
extern __declspec(dllexport) c_State state;
In "state.cpp" (from the DLL's source,) it is declared as
namespace game
{
c_State state;
//clipped for relevance
}
Could it be that I need to place the DLL somewhere specific? Does Windows know where to look? I found nowhere in the properties to specifically reference the DLL file, only the lib file.
Also, do I need a __declspec(dllexport) when declaring variables, or only functions?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将 __declspec(dllexport) 应用于定义,而不是声明。此外,该声明需要其他项目中的 __declspec(dllimport) 。因此在 .h 文件中:
在 DLL 源代码文件中:
在 DLL 项目中使用 Project + Properties、C/C++、Proprocessor。将 FOO_EXPORTS 添加到预处理器定义中。
You have to apply __declspec(dllexport) to the definition, not the declaration. Also, the declaration needs __declspec(dllimport) in the other project. So in the .h file:
In the DLL source code file:
And in the DLL project use Project + Properties, C/C++, Proprocessor. Add FOO_EXPORTS to the preprocessor definitions.