【问题标题】:Link Error 2001 when linking to User32.dll链接到 User32.dll 时出现链接错误 2001
【发布时间】:2011-11-16 06:38:16
【问题描述】:

我正在尝试链接使用在 winuser.h 中声明并在 User32.dll 中定义的两种方法的目标文件:GetMonitorInfo 和 WindowFromMonitor。源编译成目标文件就好了,但是当我尝试链接时,我得到以下错误输出:

D3dCtx.obj : error LNK2001: unresolved external symbol xGetMonitorInfo
D3dCtx.obj : error LNK2001: unresolved external symbol xMonitorFromWindow

问题是,我不叫“xGetMonitorInfo”或“xMonitorFromWindow”。对所有源文件运行 grep 显示只有“GetMonitorInfo”和“WindowFromMonitor”被调用。我正确地包括了 windows.h,其中包括 winuser.h。我还在链接器选项中正确设置了我的 LIBPATH,这由详细链接输出确认。

以下内容也出现在我的详细链接输出中:

Found __imp_GetMonitorInfoA
    Referenced in nafxcw.lib(afxribboncategory.obj)
    Referenced in nafxcw.lib(afxtooltipctrl.obj)
    Referenced in nafxcw.lib(afxribbonkeytip.obj)
    Referenced in nafxcw.lib(afxfullscreenimpl.obj)
    Referenced in nafxcw.lib(afxframeimpl.obj)
    Referenced in nafxcw.lib(afxglobalutils.obj)
    Referenced in nafxcw.lib(afxdropdowntoolbar.obj)
    Referenced in nafxcw.lib(wincore.obj)
    Referenced in nafxcw.lib(afxglobals.obj)
    Referenced in nafxcw.lib(afxpopupmenu.obj)
    Referenced in nafxcw.lib(afxpropertygridtooltipctrl.obj)
    Loaded User32.lib(USER32.dll)
Found __imp_MonitorFromWindow
    Referenced in nafxcw.lib(wincore.obj)
    Loaded User32.lib(USER32.dll)

此外,GetMonitorInfo 在 winuser.h 中定义为:

WINUSERAPI
BOOL
WINAPI
GetMonitorInfoA(
    __in HMONITOR hMonitor,
    __inout LPMONITORINFO lpmi);
WINUSERAPI
BOOL
WINAPI
GetMonitorInfoW(
    __in HMONITOR hMonitor,
    __inout LPMONITORINFO lpmi);
#ifdef UNICODE
#define GetMonitorInfo  GetMonitorInfoW
#else
#define GetMonitorInfo  GetMonitorInfoA
#endif // !UNICODE

当我将所有对“GetMonitorInfo”的引用更改为“GetMonitorInfoA”时,我只会得到

D3dCtx.obj:错误 LNK2001:无法解析的外部符号 xMonitorFromWindow

作为我的链接器错误输出。不幸的是,MonitorFromWindow 似乎没有多个版本可用。

请注意,我使用的是 64 位版本的库、链接和 cl。

这是怎么回事,如何才能成功链接我的程序?

【问题讨论】:

  • 江湖郎中喜欢宏麻烦。 grep x##
  • 好吧,我没有找到导致这种情况的宏,只有一个文件引用了这些函数。但是,您的评论确实激发了我使用宏来解决这个问题。这不是最好的解决方案(这让我有点受伤),但这段代码真的只需要编译,而不是维护。

标签: windows visual-c++ dll linker


【解决方案1】:

我不知道您是否能够找到解决方案但我遇到了同样的问题,发生这种情况的原因是我有一个名为 multimon.h 的文件

看起来在 64 位编译的情况下,由于宏定义,这些函数的定义来自两个来源,并且可能来自 multimon.h 的一个定义被覆盖并且是错误的。

我只是通过注释掉这个包含来解决它,它已经开始正常链接了。

//#include <multimon.h>

【讨论】:

  • 谢谢,我的解决方法只是在包含后重新定义宏,但很高兴知道实际的解决方案。
猜你喜欢
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
相关资源
最近更新 更多